-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQVariableDefinition.cpp
70 lines (57 loc) · 1.45 KB
/
QVariableDefinition.cpp
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
#include <assert.h>
#include <iostream>
#include "FileLexer.h"
#include "Type.h"
#include "Expression.h"
#include "CompilerHelpers.h"
#include "logging.h"
using namespace QLang;
using namespace std;
std::ostream &QLang::operator<<(std::ostream &out, const VariableDefinition &var)
{
out << var.mType << " " << var.getName();
return out;
}
VariableDefinition *VariableDefinition::ParseFuncParam( Lexer &l, Scope *s )
{
VariableDefinition *def = NULL;
SmartPtr<Type> t = Type::Parse( l, s, false );
int sym = l.getSymbol();
if ( t != NULL && sym == Lexer::SYMBOL )
{
def = new VariableDefinition( t, l.getSymbolText() );
s->addSymbol( def );
}
return def;
}
VariableDeclaration *VariableDeclaration::Parse( Lexer &l, Scope *s )
{
VariableDeclaration *def = new VariableDeclaration;
SmartPtr<Type> t = Type::Parse( l, s, false );
do {
VariableDeclaration::DeclData data;
int sym = l.getSymbol();
if ( t != NULL && sym == Lexer::SYMBOL )
{
data.mVaribale = new VariableDefinition( t, l.getSymbolText() );
}
else
{
// report error
COMPILE_ERROR( l, "Failed parse varible" );
}
s->addSymbol( data.mVaribale );
if ( l.peekSymbol() == '=' )
{
sym = l.getSymbol();
data.mInitialValue = Expression::Parse( l, s );
if ( data.mInitialValue == NULL )
{
// report error
COMPILE_ERROR( l, "Failed parse value" );
}
}
def->mVariables.push_back( data );
} while ( l.peekSymbol() == ',' );
return def;
}