-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQStructDefinition.cpp
More file actions
110 lines (85 loc) · 2.58 KB
/
QStructDefinition.cpp
File metadata and controls
110 lines (85 loc) · 2.58 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
98
99
100
101
102
103
104
105
106
107
108
109
110
#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;
StructDefinition *StructDefinition::Parse( Lexer &l, Scope *s, bool isPublic )
{
int sym = l.getSymbol();
if ( sym != Lexer::KEYWORD_STRUCT )
{
COMPILE_ERROR( l, "Internal Compiler Error" );
}
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
{
COMPILE_ERROR( l, "Expected struct name" );
}
StructDefinition *structDef = new StructDefinition( l.getSymbolText() );
structDef->mIsPublic = isPublic;
// Check for generic parameters: struct Name<T> or struct Name<T: Constraint>
if ( l.peekSymbol() == '<' )
{
l.getSymbol(); // consume '<'
do {
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
COMPILE_ERROR( l, "Expected type parameter name" );
GenericParam param;
param.mName = l.getSymbolText();
// Check for constraint: <T: Comparable>
if ( l.peekSymbol() == ':' )
{
l.getSymbol(); // consume ':'
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
COMPILE_ERROR( l, "Expected constraint name after ':'" );
param.mConstraint = l.getSymbolText();
}
structDef->mGenericParams.push_back( param );
// Register type parameter in scope
s->addType( new Type( param.mName ) );
sym = l.getSymbol();
} while ( sym == ',' );
if ( sym != '>' )
COMPILE_ERROR( l, "Expected '>' after generic parameters" );
}
sym = l.getSymbol();
if ( sym != '{' )
{
COMPILE_ERROR( l, "Expected '{' in struct definition" );
}
while ( l.peekSymbol() != '}' )
{
SmartPtr<Type> fieldType = Type::Parse( l, s, false );
if ( fieldType == nullptr )
{
COMPILE_ERROR( l, "Expected field type in struct definition" );
}
// cstring and carray types are only allowed in extern fn declarations
if ( fieldType->getName() == "cstring" || fieldType->getName() == "carray" )
COMPILE_ERROR( l, "'" + fieldType->getName() + "' type can only be used in extern fn declarations" );
sym = l.getSymbol();
if ( sym != Lexer::SYMBOL )
{
COMPILE_ERROR( l, "Expected field name in struct definition" );
}
VariableDefinition *field = new VariableDefinition( fieldType, l.getSymbolText() );
structDef->mFields.push_back( field );
sym = l.getSymbol();
if ( sym != ';' )
{
COMPILE_ERROR( l, "Expected ';' after struct field" );
}
}
sym = l.getSymbol();
assert( sym == '}' );
s->addSymbol( structDef );
s->addType( new Type( structDef->getName() ) );
cout << "Completed struct " << structDef->getName() << endl;
return structDef;
}