Skip to content

Commit

Permalink
Changed tuples to ReturnResult
Browse files Browse the repository at this point in the history
  • Loading branch information
Vardan2009 committed Aug 31, 2024
1 parent 18a5974 commit ad93812
Show file tree
Hide file tree
Showing 18 changed files with 230 additions and 112 deletions.
7 changes: 5 additions & 2 deletions eiger/Execution/BuiltInFunctions/Ascii.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ class AsciiFunction : BuiltInFunction
{
public AsciiFunction() : base("ascii", ["chr"]) { }

public override (bool, bool, Value) Execute(List<Value> args, int line, int pos, string filepath)
public override ReturnResult Execute(List<Value> args, int line, int pos, string filepath)
{
CheckArgs(filepath, line, pos, args.Count);
return (false, false, new Number(filepath, line, pos, ((String)args[0]).value[0]));
return new()
{
result = new Number(filepath, line, pos, ((String)args[0]).value[0])
};
}
}
7 changes: 5 additions & 2 deletions eiger/Execution/BuiltInFunctions/Cls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ class ClsFunction : BuiltInFunction
{
public ClsFunction() : base("cls", []) { }

public override (bool, bool, Value) Execute(List<Value> args, int line, int pos, string filepath)
public override ReturnResult Execute(List<Value> args, int line, int pos, string filepath)
{
CheckArgs(filepath, line, pos, args.Count);
Console.Clear();
return (false, false, new Nix(filepath, line, pos));
return new()
{
result = new Nix(filepath, line, pos)
};
}
}
7 changes: 5 additions & 2 deletions eiger/Execution/BuiltInFunctions/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ class ColorFunction : BuiltInFunction
{
public ColorFunction() : base("color", ["fg", "bg"]) { }

public override (bool, bool, Value) Execute(List<Value> args, int line, int pos, string filepath)
public override ReturnResult Execute(List<Value> args, int line, int pos, string filepath)
{
CheckArgs(filepath, line, pos, args.Count);
Console.ForegroundColor = (ConsoleColor)((Number)args[0]).value;
Console.BackgroundColor = (ConsoleColor)((Number)args[1]).value;
Interpreter.fgColor.value = (int)Console.ForegroundColor;
Interpreter.bgColor.value = (int)Console.BackgroundColor;
return (false, false, new Nix(filepath, line, pos));
return new()
{
result = new Nix(filepath, line, pos)
};
}
}
12 changes: 9 additions & 3 deletions eiger/Execution/BuiltInFunctions/Double.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ class DoubleFunction : BuiltInFunction
{
public DoubleFunction() : base("double", ["val"]) { }

public override (bool, bool, Value) Execute(List<Value> args, int line, int pos, string filepath)
public override ReturnResult Execute(List<Value> args, int line, int pos, string filepath)
{
CheckArgs(filepath, line, pos, args.Count);
if (args[0] is Number n)
{
return (false, true, new Number(filepath, line, pos, n.value));
return new()
{
result = new Number(filepath, line, pos, n.value)
};
}
else if (args[0] is BuiltInTypes.String s)
{
try
{
return (false, true, new Number(filepath, line, pos, Convert.ToDouble(s.value)));
return new()
{
result = new Number(filepath, line, pos, Convert.ToDouble(s.value))
};
}
catch (FormatException)
{
Expand Down
7 changes: 5 additions & 2 deletions eiger/Execution/BuiltInFunctions/Emit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ class EmitFunction : BuiltInFunction
{
public EmitFunction() : base("emit", ["value"]) { }

public override (bool, bool, Value) Execute(List<Value> args, int line, int pos, string filepath)
public override ReturnResult Execute(List<Value> args, int line, int pos, string filepath)
{
CheckArgs(filepath, line, pos, args.Count);
Console.Write(args[0]);
return (false, false, new Nix(filepath, line, pos));
return new()
{
result = new Nix(filepath, line, pos)
};
}
}
7 changes: 5 additions & 2 deletions eiger/Execution/BuiltInFunctions/Emitln.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ class EmitlnFunction : BuiltInFunction
{
public EmitlnFunction() : base("emitln", ["value"]) { }

public override (bool, bool, Value) Execute(List<Value> args, int line, int pos, string filepath)
public override ReturnResult Execute(List<Value> args, int line, int pos, string filepath)
{
CheckArgs(filepath, line, pos, args.Count);
Console.WriteLine(args[0]);
return (false, false, new Nix(filepath, line, pos));
return new()
{
result = new Nix(filepath, line, pos)
};
}
}
7 changes: 5 additions & 2 deletions eiger/Execution/BuiltInFunctions/Exit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ class ExitFunction : BuiltInFunction
{
public ExitFunction() : base("exit", ["code"]) { }

public override (bool, bool, Value) Execute(List<Value> args, int line, int pos, string filepath)
public override ReturnResult Execute(List<Value> args, int line, int pos, string filepath)
{
CheckArgs(filepath, line, pos, args.Count);
System.Environment.Exit(Convert.ToInt32(((Number)args[0]).value));
return (false, false, new Nix(filepath, line, pos));
return new()
{
result = new Nix(filepath, line, pos)
};
}
}
7 changes: 5 additions & 2 deletions eiger/Execution/BuiltInFunctions/Fread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FreadFunction : BuiltInFunction
{
public FreadFunction() : base("fread", ["path"]) { }

public override (bool, bool, Value) Execute(List<Value> args, int line, int pos, string filepath)
public override ReturnResult Execute(List<Value> args, int line, int pos, string filepath)
{
CheckArgs(filepath, line, pos, args.Count);
if (args[0] is not String)
Expand All @@ -21,7 +21,10 @@ public override (bool, bool, Value) Execute(List<Value> args, int line, int pos,
{
string pathStr = path.value;
if (File.Exists(pathStr))
return (false, false, new String(filepath, line, pos, File.ReadAllText(pathStr)));
return new()
{
result = new String(filepath, line, pos, File.ReadAllText(pathStr))
};
else
throw new Errors.EigerError(filepath, line, pos, "File doesn't exist", Errors.EigerError.ErrorType.IOError);

Expand Down
7 changes: 5 additions & 2 deletions eiger/Execution/BuiltInFunctions/In.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ class InFunction : BuiltInFunction
{
public InFunction() : base("in", []) { }

public override (bool, bool, Value) Execute(List<Value> args, int line, int pos, string filepath)
public override ReturnResult Execute(List<Value> args, int line, int pos, string filepath)
{
CheckArgs(filepath, line, pos, args.Count);
return (false, true, new BuiltInTypes.String(filepath, line, pos, Console.ReadLine() ?? ""));
return new()
{
result = new BuiltInTypes.String(filepath, line, pos, Console.ReadLine() ?? "")
};
}
}
7 changes: 5 additions & 2 deletions eiger/Execution/BuiltInFunctions/Inchar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ class IncharFunction : BuiltInFunction
{
public IncharFunction() : base("inchar", []) { }

public override (bool, bool, Value) Execute(List<Value> args, int line, int pos, string filepath)
public override ReturnResult Execute(List<Value> args, int line, int pos, string filepath)
{
CheckArgs(filepath, line, pos, args.Count);
return (false, true, new BuiltInTypes.String(filepath, line, pos, Console.ReadKey().KeyChar.ToString()));
return new()
{
result = new BuiltInTypes.String(filepath, line, pos, Console.ReadKey().KeyChar.ToString())
};
}
}
12 changes: 9 additions & 3 deletions eiger/Execution/BuiltInFunctions/Int.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ class IntFunction : BuiltInFunction
{
public IntFunction() : base("int", ["val"]) { }

public override (bool, bool, Value) Execute(List<Value> args, int line, int pos, string filepath)
public override ReturnResult Execute(List<Value> args, int line, int pos, string filepath)
{
CheckArgs(filepath, line, pos, args.Count);
if (args[0] is Number n)
{
return (false, true, new Number(filepath, line, pos, Convert.ToInt32(n.value)));
return new()
{
result = new Number(filepath, line, pos, Convert.ToInt32(n.value))
};
}
else if (args[0] is BuiltInTypes.String s)
{
try
{
return (false, true, new Number(filepath, line, pos, Convert.ToInt32(Convert.ToDouble(s.value))));
return new()
{
result = new Number(filepath, line, pos, Convert.ToInt32(Convert.ToDouble(s.value)))
};
}
catch (FormatException)
{
Expand Down
7 changes: 5 additions & 2 deletions eiger/Execution/BuiltInFunctions/Rand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ class RandFunction : BuiltInFunction
public RandFunction() : base("rand", []) { }
readonly Random rand = new();

public override (bool, bool, Value) Execute(List<Value> args, int line, int pos, string filepath)
public override ReturnResult Execute(List<Value> args, int line, int pos, string filepath)
{
CheckArgs(filepath, line, pos, args.Count);
return (false, false, new Number(filepath, line, pos, rand.NextDouble()));
return new()
{
result = new Number(filepath, line, pos, rand.NextDouble())
};
}
}
7 changes: 5 additions & 2 deletions eiger/Execution/BuiltInFunctions/Time.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ class TimeFunction : BuiltInFunction
{
public TimeFunction() : base("time", []) { }

public override (bool, bool, Value) Execute(List<Value> args, int line, int pos, string filepath)
public override ReturnResult Execute(List<Value> args, int line, int pos, string filepath)
{
CheckArgs(filepath, line, pos, args.Count);
DateTimeOffset now = DateTimeOffset.Now;
DateTimeOffset epoch = new(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
return (false, false, new Number(filepath, line, pos, (now - epoch).TotalSeconds));
return new()
{
result = new Number(filepath, line, pos, (now - epoch).TotalSeconds)
};
}
}
4 changes: 2 additions & 2 deletions eiger/Execution/BuiltInTypes/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Class(string filename, int line, int pos, string name, Dictionary<string,
this.blockNode = blockNode;
}

public (bool, bool, Instance) Execute(List<Value> args)
public ReturnResult Execute(List<Value> args)
{
// create local symbol table
Dictionary<string, Value> localSymbolTable = new(symbolTable);
Expand All @@ -37,7 +37,7 @@ public Class(string filename, int line, int pos, string name, Dictionary<string,
if (value is Function constructorFunc)
constructorFunc.Execute(args, line, pos, filename);

return (false, false, inst);
return new() { result = inst };
}

public override Value GetAttr(ASTNode attr)
Expand Down
13 changes: 7 additions & 6 deletions eiger/Execution/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void CheckArgs(string path, int line, int pos, int argCount)
throw new EigerError(path, line, pos, $"Function {name} takes {arg_n.Count} arguments, got {argCount}", EigerError.ErrorType.ArgumentError);
}

public abstract (bool, bool, Value) Execute(List<Value> args, int line, int pos, string path); // abstract execute function
public abstract ReturnResult Execute(List<Value> args, int line, int pos, string path); // abstract execute function
}

// custom functions
Expand All @@ -37,7 +37,7 @@ public Function(ASTNode node, string name, List<string> arg_n, ASTNode root, Dic
this.root = root;
}

public override (bool, bool, Value) Execute(List<Value> args, int line, int pos, string path)
public override ReturnResult Execute(List<Value> args, int line, int pos, string path)
{
CheckArgs(path, line, pos, args.Count);

Expand Down Expand Up @@ -71,7 +71,7 @@ public InlineFunction(ASTNode node, string name, List<string> arg_n, ASTNode roo
this.root = root;
}

public override (bool, bool, Value) Execute(List<Value> args, int line, int pos, string path)
public override ReturnResult Execute(List<Value> args, int line, int pos, string path)
{
CheckArgs(path, line, pos, args.Count);

Expand All @@ -85,8 +85,9 @@ public override (bool, bool, Value) Execute(List<Value> args, int line, int pos,
}

// visit the body and return the result
(bool shouldBreak, bool shouldReturn, Value v) = Interpreter.VisitNode(root, localSymbolTable);
return (shouldBreak, true, v);
ReturnResult r = Interpreter.VisitNode(root, localSymbolTable);
r.shouldReturn = true;
return r;
}

public override string ToString()
Expand All @@ -101,7 +102,7 @@ abstract class BuiltInFunction : BaseFunction
{
public BuiltInFunction(string name, List<string> arg_n) : base(new(NodeType.Block, 0, 0, 0, "<unset>"), name, arg_n, Interpreter.globalSymbolTable) { }

public override (bool, bool, Value) Execute(List<Value> args, int line, int pos, string file) { return (false, false, new Nix(file, line, pos)); }
public override ReturnResult Execute(List<Value> args, int line, int pos, string file) { return new() { result = new Nix(file, line, pos) }; }

public override string ToString()
{
Expand Down
Loading

0 comments on commit ad93812

Please sign in to comment.