Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/eigerproject/eigercs
Browse files Browse the repository at this point in the history
  • Loading branch information
Vardan2009 committed Aug 21, 2024
2 parents 39e2bf7 + 51b8777 commit 49701f4
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
let name
let surname
func Person(name, surname)
~ constructor
func new(name, surname)
this.name = name
this.surname = surname
end
Expand Down
4 changes: 2 additions & 2 deletions eiger.Tests/EigerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void OOPInlineTest()
"class Person\n" +
" let name" +
" let surname" +
" func Person(name, surname)\n" +
" func new(name, surname)\n" +
" this.name = name\n" +
" this.surname = surname\n" +
" end\n" +
Expand All @@ -158,7 +158,7 @@ public void OOPClassTests()
"class Person\n" +
" let name" +
" let surname" +
" func Person(name, surname)\n" +
" func new(name, surname)\n" +
" this.name = name\n" +
" this.surname = surname\n" +
" end\n" +
Expand Down
4 changes: 3 additions & 1 deletion eiger/Execution/BuiltInTypes/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class Class : Value

public Class(string filename, int line, int pos, string name, Dictionary<string, Value> symbolTable, ASTNode blockNode) : base(filename, line, pos)
{
if (name == "new")
throw new EigerLang.Errors.EigerError(filename, line, pos, "`new` is an invalid name for a class", Errors.EigerError.ErrorType.RuntimeError);
this.filename = filename;
this.line = line;
this.pos = pos;
Expand All @@ -31,7 +33,7 @@ public Class(string filename, int line, int pos, string name, Dictionary<string,

localSymbolTable = Interpreter.GetDictionaryDifference(symbolTable, localSymbolTable);

if (localSymbolTable.TryGetValue(name, out Value value))
if (localSymbolTable.TryGetValue("new", out Value value))

Check warning on line 36 in eiger/Execution/BuiltInTypes/Class.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 36 in eiger/Execution/BuiltInTypes/Class.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
if (value is Function constructorFunc)
constructorFunc.Execute(args, line, pos, filename);

Expand Down
12 changes: 9 additions & 3 deletions eiger/Execution/BuiltInTypes/Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace EigerLang.Execution.BuiltInTypes;

public class Value(string filename, int line, int pos)
public class Value(string _filename, int _line, int _pos)

Check warning on line 6 in eiger/Execution/BuiltInTypes/Value.cs

View workflow job for this annotation

GitHub Actions / build

'Value' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 6 in eiger/Execution/BuiltInTypes/Value.cs

View workflow job for this annotation

GitHub Actions / build

'Value' overrides Object.Equals(object o) but does not override Object.GetHashCode()
{
public bool isReadonly = false;
public string filename;
public int line, pos;
public string filename = _filename;
public int line = _line, pos = _pos;

public virtual Value AddedTo(object other)
{
Expand Down Expand Up @@ -50,11 +50,17 @@ public virtual Value Negative()

public virtual Boolean ComparisonEqeq(object other)
{
if (other is Nix)
return new Boolean(filename, line, pos, this is Nix);

throw new EigerError(filename, line, pos, $"{Globals.InvalidOperationStr}: {this.GetType().Name} ?= {other.GetType().Name}", EigerError.ErrorType.InvalidOperationError);
}

public virtual Boolean ComparisonNeqeq(object other)
{
if (other is Nix)
return new Boolean(filename, line, pos, this is not Nix);

throw new EigerError(filename, line, pos, $"{Globals.InvalidOperationStr}: {this.GetType().Name} != {other.GetType().Name}", EigerError.ErrorType.InvalidOperationError);
}

Expand Down
19 changes: 16 additions & 3 deletions eiger/Execution/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary<string
// get function name
string funcName = node.value ?? "";

// a symbol with that name already exists
if (symbolTable.ContainsKey(funcName) && funcName != "")
throw new EigerError(node.filename, node.line, node.pos, $"{funcName} already declared", EigerError.ErrorType.RuntimeError);

// get funciton body
ASTNode root = node.children[0];

Expand Down Expand Up @@ -390,6 +394,9 @@ public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary<string
// get function name
string funcName = node.value ?? "";

if (symbolTable.ContainsKey(funcName) && funcName != "")
throw new EigerError(node.filename, node.line, node.pos, $"{funcName} already declared", EigerError.ErrorType.RuntimeError);

// get funciton body
ASTNode root = node.children[0];

Expand All @@ -405,8 +412,14 @@ public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary<string
argnames.Add(node.children[i].value);
}

InlineFunction f = new InlineFunction(node, funcName, argnames, root, symbolTable);

// add the function to the current scope
if (funcName != "")
symbolTable[funcName] = f;

// return the function
return (false, false, new InlineFunction(node, funcName, argnames, root, symbolTable));
return (false, false, f);
}

// visit unary operator node
Expand Down Expand Up @@ -727,12 +740,12 @@ public static void SetSymbol(Dictionary<string, Value> symbolTable, ASTNode key,
}
else
{
throw new EigerError(key.filename, key.line, key.pos, "Assignee must be identifier", EigerError.ErrorType.RuntimeError);
throw new EigerError(key.filename, key.line, key.pos, "Left side of assignment is invalid", EigerError.ErrorType.RuntimeError);
}
}
catch (KeyNotFoundException)
{
throw new EigerError(key.filename, key.line, key.pos, $"Variable is undefined", EigerError.ErrorType.RuntimeError);
throw new EigerError(key.filename, key.line, key.pos, "Variable is undefined", EigerError.ErrorType.RuntimeError);
}
}
}
7 changes: 5 additions & 2 deletions eiger/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,26 @@ public static void Execute(string src, string fn, bool printExprs)
(bool shouldBreak, bool didReturn, Value val) = Interpreter.VisitNode(statement, Interpreter.globalSymbolTable);
if (printExprs)
{
string v = Convert.ToString(val) ?? "";
switch (val.GetType().Name)
{
case "Number":
case "Boolean":
Console.ForegroundColor = ConsoleColor.Cyan; break;
case "String":
Console.ForegroundColor = ConsoleColor.Yellow; break;
Console.ForegroundColor = ConsoleColor.Yellow; v = $"\"{v}\""; break;
case "Nix":
Console.ForegroundColor = ConsoleColor.DarkGray; break;
case "Function":
case "InlineFunction":
case "Class":
case "Instance":
case "Dataclass":
Console.ForegroundColor = ConsoleColor.Green; break;
default:
Console.ResetColor(); break;
}
Console.WriteLine(Convert.ToString(val));
Console.WriteLine(v);
Console.ResetColor();
}
}
Expand Down

0 comments on commit 49701f4

Please sign in to comment.