Skip to content

Commit 60a09fc

Browse files
authored
Merge pull request #2 from helperf/main
added "typedef" operator
2 parents 1141fcb + 10d6f70 commit 60a09fc

File tree

4 files changed

+118
-4
lines changed

4 files changed

+118
-4
lines changed

SourceCode/Parser/Lexer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ private void tokenizeWord()
187187
string word = buffer.ToString();
188188
switch (word)
189189
{
190+
case "typedef":
191+
{
192+
addToken(TokenType.TYPEDEF);
193+
break;
194+
}
190195
case "inst":
191196
{
192197
addToken(TokenType.INST_ENUM);

SourceCode/Parser/Parser.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,59 @@ private Statement assignmentStatement()
202202
return new AssignmentStatement(variable, expression());
203203
}
204204

205+
if (current.getType() == TokenType.TYPEDEF)
206+
{
207+
consume(TokenType.TYPEDEF);
208+
string type = consume(TokenType.WORD).getText();
209+
string variable = consume(TokenType.WORD).getText();
210+
consume(TokenType.EQ);
211+
AssignmentStatement assignmentStatement = new AssignmentStatement(variable, expression(), true);
212+
switch (type)
213+
{
214+
case "int":
215+
{
216+
assignmentStatement.value = new NumberValue(0);
217+
break;
218+
}
219+
case "enum":
220+
{
221+
assignmentStatement.value = new EnumValue(new Dictionary<string, Value>());
222+
break;
223+
}
224+
case "string":
225+
{
226+
assignmentStatement.value = new StringValue(null);
227+
break;
228+
}
229+
case "dict":
230+
{
231+
assignmentStatement.value = new DictionaryValue(0);
232+
break;
233+
}
234+
case "stack":
235+
{
236+
assignmentStatement.value = new StackValue(0);
237+
break;
238+
}
239+
case "bool":
240+
{
241+
assignmentStatement.value = new BoolValue(true);
242+
break;
243+
}
244+
case "obj":
245+
{
246+
assignmentStatement.value = new ObjectValue(null);
247+
break;
248+
}
249+
case "arr":
250+
{
251+
assignmentStatement.value = new ArrayValue(0);
252+
break;
253+
}
254+
}
255+
return assignmentStatement;
256+
}
257+
205258
if (current.getType() == TokenType.WORD && get(1).getType() == TokenType.WORD && get(2).getType() == TokenType.DDOTEQ)
206259
{
207260
string enums = consume(TokenType.WORD).getText();

SourceCode/Parser/TokenType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public enum TokenType
4545
STATEMENT,
4646
SWITCH,
4747
CASE,
48+
TYPEDEF,
4849

4950
STAREQ,
5051
SLASHEQ,

SourceCode/ast/AssignmentStatement.cs

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using OwnLang.ast.lib;
1+
using OwnLang.ast.lib;
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
@@ -11,17 +11,72 @@ class AssignmentStatement : Statement
1111
{
1212
private string variable;
1313
private Expression expression;
14+
private bool isTypedef;
15+
public Value value;
1416

15-
public AssignmentStatement(string variable, Expression expression)
17+
public AssignmentStatement(string variable, Expression expression, bool isTypedef = false)
1618
{
1719
this.variable = variable;
1820
this.expression = expression;
21+
this.isTypedef = isTypedef;
1922
}
2023

2124
public void execute()
2225
{
23-
// expression.eval()
24-
Value result = expression.eval();
26+
Value result;
27+
if (isTypedef)
28+
{
29+
if (value is NumberValue)
30+
{
31+
result = (NumberValue)expression.eval();
32+
Variables.set(variable, result);
33+
return;
34+
}
35+
if (value is BoolValue)
36+
{
37+
result = (BoolValue)expression.eval();
38+
Variables.set(variable, result);
39+
return;
40+
}
41+
if (value is StringValue)
42+
{
43+
result = (StringValue)expression.eval();
44+
Variables.set(variable, result);
45+
return;
46+
}
47+
if (value is EnumValue)
48+
{
49+
result = (EnumValue)expression.eval();
50+
Variables.set(variable, result);
51+
return;
52+
}
53+
if (value is ObjectValue)
54+
{
55+
result = (ObjectValue)expression.eval();
56+
Variables.set(variable, result);
57+
return;
58+
}
59+
if (value is ArrayValue)
60+
{
61+
result = (ArrayValue)expression.eval();
62+
Variables.set(variable, result);
63+
return;
64+
}
65+
if (value is StackValue)
66+
{
67+
result = (StackValue)expression.eval();
68+
Variables.set(variable, result);
69+
return;
70+
}
71+
if (value is DictionaryValue)
72+
{
73+
result = (DictionaryValue)expression.eval();
74+
Variables.set(variable, result);
75+
return;
76+
}
77+
78+
}
79+
result = expression.eval();
2580
Variables.set(variable, result);
2681
}
2782

0 commit comments

Comments
 (0)