-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQForInStatement.cpp
More file actions
97 lines (79 loc) · 2.47 KB
/
QForInStatement.cpp
File metadata and controls
97 lines (79 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <assert.h>
#include <iostream>
#include "FileLexer.h"
#include "Type.h"
#include "Expression.h"
#include "CompilerHelpers.h"
#include "logging.h"
SET_LOG_CAT( LOG_CAT_ALL );
SET_LOG_LEVEL( LOG_LVL_NOISE );
using namespace QLang;
using namespace std;
// Parse for-in loop: for VAR in EXPR { BODY }
// Parse for-in with two vars: for KEY, VALUE in EXPR { BODY }
// Parse infinite loop: for { BODY }
ForInStatement *ForInStatement::Parse( Lexer &l, Scope *scope )
{
TRACE_BEGIN( LOG_LVL_INFO );
// Consume 'for' keyword
int sym = l.getSymbol();
if ( sym != Lexer::KEYWORD_FOR )
{
COMPILE_ERROR( l, "Internal Error: expected 'for' keyword" );
}
ForInStatement *statement = new ForInStatement;
Scope *loop_scope = new Scope( Scope::kScope_Loop );
loop_scope->setParent( scope );
// Check for infinite loop: for { body }
if ( l.peekSymbol() == '{' )
{
statement->mIsInfinite = true;
statement->mBody = Block::Parse( l, loop_scope );
return statement;
}
// Parse loop variable name
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
{
COMPILE_ERROR( l, "Expected variable name in for-in loop" );
}
statement->mVariableName = l.getSymbolText();
// Check for second variable: for key, value in expr
if ( l.peekSymbol() == ',' )
{
l.getSymbol(); // consume ','
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
{
COMPILE_ERROR( l, "Expected second variable name after ',' in for-in loop" );
}
statement->mSecondVariableName = l.getSymbolText();
}
// Expect 'in' keyword
sym = l.getSymbol();
if ( sym != Lexer::KEYWORD_IN )
{
COMPILE_ERROR( l, "Expected 'in' keyword in for-in loop" );
}
// Add loop variable(s) to scope as variables
Type *varType = new Type( "var" );
VariableDefinition *loopVar = new VariableDefinition( varType, statement->mVariableName );
loop_scope->addSymbol( loopVar );
if ( !statement->mSecondVariableName.empty() )
{
VariableDefinition *loopVar2 = new VariableDefinition( varType, statement->mSecondVariableName );
loop_scope->addSymbol( loopVar2 );
}
// Parse the iterable expression (could be a range like 0..10 or a variable)
statement->mIterableExpression = Expression::ParseExpr( l, loop_scope, 0 );
if ( statement->mIterableExpression == nullptr )
{
COMPILE_ERROR( l, "Expected expression after 'in' in for-in loop" );
}
// Parse the loop body
if ( l.peekSymbol() == '{' )
statement->mBody = Block::Parse( l, loop_scope );
else
statement->mBody = Statement::Parse( l, loop_scope );
return statement;
}