Skip to content

Commit

Permalink
Merge pull request #2 from helperf/main
Browse files Browse the repository at this point in the history
added "typedef" operator
  • Loading branch information
bas1c1 authored Dec 13, 2022
2 parents 1141fcb + 10d6f70 commit 60a09fc
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 4 deletions.
5 changes: 5 additions & 0 deletions SourceCode/Parser/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ private void tokenizeWord()
string word = buffer.ToString();
switch (word)
{
case "typedef":
{
addToken(TokenType.TYPEDEF);
break;
}
case "inst":
{
addToken(TokenType.INST_ENUM);
Expand Down
53 changes: 53 additions & 0 deletions SourceCode/Parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,59 @@ private Statement assignmentStatement()
return new AssignmentStatement(variable, expression());
}

if (current.getType() == TokenType.TYPEDEF)
{
consume(TokenType.TYPEDEF);
string type = consume(TokenType.WORD).getText();
string variable = consume(TokenType.WORD).getText();
consume(TokenType.EQ);
AssignmentStatement assignmentStatement = new AssignmentStatement(variable, expression(), true);
switch (type)
{
case "int":
{
assignmentStatement.value = new NumberValue(0);
break;
}
case "enum":
{
assignmentStatement.value = new EnumValue(new Dictionary<string, Value>());
break;
}
case "string":
{
assignmentStatement.value = new StringValue(null);
break;
}
case "dict":
{
assignmentStatement.value = new DictionaryValue(0);
break;
}
case "stack":
{
assignmentStatement.value = new StackValue(0);
break;
}
case "bool":
{
assignmentStatement.value = new BoolValue(true);
break;
}
case "obj":
{
assignmentStatement.value = new ObjectValue(null);
break;
}
case "arr":
{
assignmentStatement.value = new ArrayValue(0);
break;
}
}
return assignmentStatement;
}

if (current.getType() == TokenType.WORD && get(1).getType() == TokenType.WORD && get(2).getType() == TokenType.DDOTEQ)
{
string enums = consume(TokenType.WORD).getText();
Expand Down
1 change: 1 addition & 0 deletions SourceCode/Parser/TokenType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public enum TokenType
STATEMENT,
SWITCH,
CASE,
TYPEDEF,

STAREQ,
SLASHEQ,
Expand Down
63 changes: 59 additions & 4 deletions SourceCode/ast/AssignmentStatement.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OwnLang.ast.lib;
using OwnLang.ast.lib;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -11,17 +11,72 @@ class AssignmentStatement : Statement
{
private string variable;
private Expression expression;
private bool isTypedef;
public Value value;

public AssignmentStatement(string variable, Expression expression)
public AssignmentStatement(string variable, Expression expression, bool isTypedef = false)
{
this.variable = variable;
this.expression = expression;
this.isTypedef = isTypedef;
}

public void execute()
{
// expression.eval()
Value result = expression.eval();
Value result;
if (isTypedef)
{
if (value is NumberValue)
{
result = (NumberValue)expression.eval();
Variables.set(variable, result);
return;
}
if (value is BoolValue)
{
result = (BoolValue)expression.eval();
Variables.set(variable, result);
return;
}
if (value is StringValue)
{
result = (StringValue)expression.eval();
Variables.set(variable, result);
return;
}
if (value is EnumValue)
{
result = (EnumValue)expression.eval();
Variables.set(variable, result);
return;
}
if (value is ObjectValue)
{
result = (ObjectValue)expression.eval();
Variables.set(variable, result);
return;
}
if (value is ArrayValue)
{
result = (ArrayValue)expression.eval();
Variables.set(variable, result);
return;
}
if (value is StackValue)
{
result = (StackValue)expression.eval();
Variables.set(variable, result);
return;
}
if (value is DictionaryValue)
{
result = (DictionaryValue)expression.eval();
Variables.set(variable, result);
return;
}

}
result = expression.eval();
Variables.set(variable, result);
}

Expand Down

0 comments on commit 60a09fc

Please sign in to comment.