-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQLambdaExpression.cpp
More file actions
61 lines (51 loc) · 1.56 KB
/
QLambdaExpression.cpp
File metadata and controls
61 lines (51 loc) · 1.56 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
#include <iostream>
#include "FileLexer.h"
#include "Type.h"
#include "Expression.h"
#include "CompilerHelpers.h"
using namespace QLang;
using namespace std;
LambdaExpression *LambdaExpression::Parse( Lexer &l, Scope *scope )
{
// 'fn' already consumed by caller (ParsePrimary)
// Parse '('
int sym = l.getSymbol();
if ( sym != '(' )
COMPILE_ERROR( l, "Expected '(' in lambda expression" );
LambdaExpression *lambda = new LambdaExpression();
// Create a child scope for the lambda body
SmartPtr<Scope> lambdaScope = new Scope( Scope::kScope_Function );
lambdaScope->setParent( scope );
// Parse parameters
if ( l.peekSymbol() != ')' )
{
int paramIndex = 0;
do {
VariableDefinition *param = VariableDefinition::ParseFuncParam( l, lambdaScope, false, paramIndex++ );
if ( param == nullptr )
COMPILE_ERROR( l, "Expected parameter in lambda expression" );
lambda->mParameters.push_back( param );
sym = l.getSymbol();
if ( sym == ')' ) break;
if ( sym != ',' )
COMPILE_ERROR( l, "Expected ',' or ')' in lambda parameters" );
} while ( true );
}
else
{
l.getSymbol(); // consume ')'
}
// Optional return type: -> Type
if ( l.peekSymbol() == Lexer::ARROW )
{
l.getSymbol(); // consume '->'
lambda->mReturnType = Type::Parse( l, lambdaScope, false );
if ( lambda->mReturnType == nullptr )
COMPILE_ERROR( l, "Expected return type after '->' in lambda" );
}
// Parse body block
lambda->mBody = Block::Parse( l, lambdaScope );
if ( lambda->mBody == nullptr )
COMPILE_ERROR( l, "Expected '{' for lambda body" );
return lambda;
}