Skip to content

Commit d2df378

Browse files
committed
Variables declarations and initializations added (previously forgotten)
1 parent 4ac09c2 commit d2df378

4 files changed

Lines changed: 40 additions & 8 deletions

File tree

antlr/GreekBaseLexer.g4

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ KW_USE : 'use';
2222
KW_WITH : 'with';
2323
KW_NEW : 'new';
2424
KW_PRINT : 'print';
25-
KW_LCURL : '{';
26-
KW_RCURL : '}';
25+
KW_LCURL : '{';
26+
KW_RCURL : '}';
27+
KW_INT : 'int';
28+
KW_FLOAT : 'float';
29+
KW_CHAR : 'char';
30+
KW_STRING : 'string';
2731

2832
// Symbols and operators
2933
OP_ASSIGN : ':=';

antlr/GreekBaseParser.g4

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ assignment
7272
;
7373

7474
variableDeclaration
75-
: literal IDENTIFIER OP_SEMICOLON;
75+
: IDENTIFIER OP_COLON varType (OP_ASSIGN expression)? OP_SEMICOLON;
7676

7777

7878
// ----procedure specific things----
@@ -135,3 +135,10 @@ relop
135135
| OP_GREATER
136136
| OP_GREATER_EQ
137137
;
138+
139+
varType
140+
: KW_INT
141+
| KW_FLOAT
142+
| KW_CHAR
143+
| KW_STRING
144+
;

src/astGreek.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class Literal(Expression):
2626
class RelationOperator(ASTNode):
2727
pass
2828

29+
class VariableType(ASTNode):
30+
pass
31+
2932

3033
# LITERALS
3134
@dataclass
@@ -148,5 +151,6 @@ class Procedure(Statement):
148151
# variables
149152
@dataclass
150153
class VariableDeclaration(NonDeclarativeStatement):
151-
varType: Literal
152-
id: Identifier
154+
varType: VariableType
155+
id: Identifier
156+
varValue : Literal

src/ast_builder.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,26 @@ def visitProgram(self, ctx: GreekBaseParser.ProgramContext):
1010
return ast.Program(ctx.start.line, ctx.start.column, [self.visit(stmt) for stmt in ctx.statement()])
1111

1212
def visitVariableDeclaration(self, ctx: GreekBaseParser.VariableDeclarationContext):
13-
var_name = ctx.Identifier().getText()
14-
var_type = self.visit(ctx.literal())
15-
return ast.VariableDeclaration(ctx.start.line, ctx.start.column, var_type, var_name)
13+
var_name = ctx.IDENTIFIER().getText()
14+
var_type = self.visit(ctx.varType())
15+
if ctx.expression():
16+
var_value = self.visit(ctx.expression())
17+
else:
18+
var_value = None
19+
return ast.VariableDeclaration(ctx.start.line, ctx.start.column, var_type, var_name, var_value)
20+
21+
def visitVarType(self, ctx: GreekBaseParser.VarTypeContext):
22+
if ctx.KW_INT():
23+
return int
24+
elif ctx.KW_FLOAT():
25+
return float
26+
elif ctx.KW_CHAR():
27+
return str
28+
elif ctx.KW_STRING():
29+
return str
30+
else:
31+
return None
32+
1633

1734
def visitAssignment(self, ctx: GreekBaseParser.AssignmentContext):
1835
# Handles: IDENTIFIER := expression;

0 commit comments

Comments
 (0)