Mercurial > projects > dang
annotate sema/ScopeBuilder.d @ 158:57b0b4464a0b
Parsing "new", putting it in AST and performs some tests on it. Eg. if the contructor exists and the params matches.
author | Anders Johnsen <skabet@gmail.com> |
---|---|
date | Tue, 22 Jul 2008 00:33:58 +0200 |
parents | 393a1f47a6d2 |
children | 0f38f1a0f06f |
rev | line source |
---|---|
92
771ac63898e2
A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents:
86
diff
changeset
|
1 module sema.ScopeBuilder; |
1 | 2 |
14 | 3 import tango.io.Stdout, |
4 tango.core.Array : find; | |
1 | 5 |
26 | 6 public |
129 | 7 import sema.Scope, |
8 sema.Symbol; | |
1 | 9 |
59
1d6f4ad38a91
Make most of the tests pass again
Anders Halager <halager@gmail.com>
parents:
56
diff
changeset
|
10 import sema.Visitor, |
1d6f4ad38a91
Make most of the tests pass again
Anders Halager <halager@gmail.com>
parents:
56
diff
changeset
|
11 basic.SmallArray; |
1 | 12 |
92
771ac63898e2
A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
Anders Johnsen <skabet@gmail.com>
parents:
86
diff
changeset
|
13 class ForwardReference : Visitor!(void) |
1 | 14 { |
101
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
15 override void visit(Module[] modules) |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
16 { |
133
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
17 (new TypeBuilder).visit(modules); |
101
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
18 this.modules = modules; |
129 | 19 inFunctionBodyStack.push(false); |
20 foreach (mod; modules) | |
21 { | |
22 current = mod; | |
23 foreach (decl; mod.decls) | |
24 visitDecl(decl); | |
25 } | |
101
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
26 } |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
27 |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
28 override void visitFuncDecl(FuncDecl d) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
29 { |
129 | 30 |
63
9f8131676242
Now Decl's have a DType type(), and should use varType and returnType to get the old type id
Anders Halager <halager@gmail.com>
parents:
59
diff
changeset
|
31 visitExp(d.returnType); |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
32 visitExp(d.identifier); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
33 foreach (arg; d.funcArgs) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
34 visitDecl(arg); |
129 | 35 |
36 inFunctionBodyStack.push(true); | |
37 | |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
38 foreach (stmt; d.statements) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
39 visitStmt(stmt); |
59
1d6f4ad38a91
Make most of the tests pass again
Anders Halager <halager@gmail.com>
parents:
56
diff
changeset
|
40 |
129 | 41 inFunctionBodyStack.pop(); |
42 | |
136 | 43 d.sym = current.symbol.createMember( |
44 d.identifier.get, | |
45 d.type, | |
158
57b0b4464a0b
Parsing "new", putting it in AST and performs some tests on it. Eg. if the contructor exists and the params matches.
Anders Johnsen <skabet@gmail.com>
parents:
149
diff
changeset
|
46 d); |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
47 } |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
48 |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
49 override void visitVarDecl(VarDecl d) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
50 { |
82
06dda301ea61
Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents:
81
diff
changeset
|
51 visitExp(d.varType); |
86
29f486ccc203
Fixed a bug that made arrays as params fail big time
Anders Johnsen <skabet@gmail.com>
parents:
83
diff
changeset
|
52 visitExp(d.identifier); |
82
06dda301ea61
Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents:
81
diff
changeset
|
53 |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
54 if (d.init) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
55 visitExp(d.init); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
56 |
129 | 57 DType t = typeOf(d.varType, d.env); |
136 | 58 d.sym = current.symbol.createAlias( |
59 d.identifier.get, | |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
144
diff
changeset
|
60 d.env.find(d.varType.get).sym, |
158
57b0b4464a0b
Parsing "new", putting it in AST and performs some tests on it. Eg. if the contructor exists and the params matches.
Anders Johnsen <skabet@gmail.com>
parents:
149
diff
changeset
|
61 d); |
136 | 62 d.sym.type = t; |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
63 } |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
64 |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
65 override void visitStructDecl(StructDecl s) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
66 { |
129 | 67 auto old = current.symbol; |
68 current.symbol = s.sym; | |
69 inFunctionBodyStack.push(false); | |
70 super.visitStructDecl(s); | |
71 inFunctionBodyStack.pop(); | |
72 current.symbol = old; | |
133
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
73 } |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
74 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
75 override void visitClassDecl(ClassDecl s) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
76 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
77 auto old = current.symbol; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
78 current.symbol = s.sym; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
79 inFunctionBodyStack.push(false); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
80 super.visitClassDecl(s); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
81 inFunctionBodyStack.pop(); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
82 current.symbol = old; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
83 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
84 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
85 override void visitInterfaceDecl(InterfaceDecl s) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
86 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
87 auto old = current.symbol; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
88 current.symbol = s.sym; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
89 inFunctionBodyStack.push(false); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
90 super.visitInterfaceDecl(s); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
91 inFunctionBodyStack.pop(); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
92 current.symbol = old; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
93 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
94 |
133
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
95 DType typeOf(Identifier id, Scope sc) |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
96 { |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
97 if(auto i = cast(PointerIdentifier)id) |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
98 return (typeOf(i.pointerOf, sc)).getPointerTo(); |
136 | 99 else if(auto i = cast(StaticArrayIdentifier)id) |
100 return typeOf(i.arrayOf, sc).getAsStaticArray(i.size); | |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
144
diff
changeset
|
101 return sc.findType(id.get); |
133
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
102 } |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
103 |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
104 Module[] modules; |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
105 Module current; |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
106 SmallArray!(bool) inFunctionBodyStack; |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
107 } |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
108 |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
109 class TypeBuilder : Visitor!(void) |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
110 { |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
111 override void visit(Module[] modules) |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
112 { |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
113 foreach (mod; modules) |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
114 { |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
115 current = mod; |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
116 foreach (decl; mod.decls) |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
117 visitDecl(decl); |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
118 } |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
119 } |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
120 |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
121 override void visitStructDecl(StructDecl s) |
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
122 { |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
144
diff
changeset
|
123 auto st = s.env.findType(s.identifier.get).asStruct; |
136 | 124 s.sym = current.symbol.createMember( |
125 s.identifier.get, | |
126 st, | |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
144
diff
changeset
|
127 s.env.find(s.identifier.get)); |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
53
diff
changeset
|
128 |
107
189c049cbfcc
Cleanup of codegen, better support for operators a few bugfixes
Anders Halager <halager@gmail.com>
parents:
103
diff
changeset
|
129 foreach (decl; s.decls) |
129 | 130 { |
131 DType type; | |
132 char[] name; | |
107
189c049cbfcc
Cleanup of codegen, better support for operators a few bugfixes
Anders Halager <halager@gmail.com>
parents:
103
diff
changeset
|
133 if (auto varDecl = cast(VarDecl)decl) |
129 | 134 { |
135 type = typeOf(varDecl.varType, varDecl.env); | |
136 name = varDecl.identifier.get; | |
137 } | |
107
189c049cbfcc
Cleanup of codegen, better support for operators a few bugfixes
Anders Halager <halager@gmail.com>
parents:
103
diff
changeset
|
138 else if (auto fd = cast(FuncDecl)decl) |
129 | 139 { |
140 type = fd.type; | |
141 name = fd.identifier.get; | |
142 } | |
143 st.addMember(type, name); | |
144 } | |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
145 } |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
146 |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
147 override void visitClassDecl(ClassDecl s) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
148 { |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
144
diff
changeset
|
149 auto st = s.env.findType(s.identifier.get).asClass; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
150 s.sym = current.symbol.createMember( |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
151 s.identifier.get, |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
152 st, |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
144
diff
changeset
|
153 s.env.find(s.identifier.get)); |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
154 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
155 foreach (decl; s.decls) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
156 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
157 DType type; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
158 char[] name; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
159 if (auto varDecl = cast(VarDecl)decl) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
160 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
161 type = typeOf(varDecl.varType, varDecl.env); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
162 name = varDecl.identifier.get; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
163 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
164 else if (auto fd = cast(FuncDecl)decl) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
165 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
166 type = fd.type; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
167 name = fd.identifier.get; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
168 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
169 st.addMember(type, name); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
170 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
171 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
172 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
173 override void visitInterfaceDecl(InterfaceDecl s) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
174 { |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
144
diff
changeset
|
175 auto st = s.env.findType(s.identifier.get).asInterface; |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
176 s.sym = current.symbol.createMember( |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
177 s.identifier.get, |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
178 st, |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
144
diff
changeset
|
179 s.env.find(s.identifier.get)); |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
180 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
181 foreach (decl; s.decls) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
182 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
183 DType type; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
184 char[] name; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
185 if (auto varDecl = cast(VarDecl)decl) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
186 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
187 type = typeOf(varDecl.varType, varDecl.env); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
188 name = varDecl.identifier.get; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
189 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
190 else if (auto fd = cast(FuncDecl)decl) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
191 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
192 type = fd.type; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
193 name = fd.identifier.get; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
194 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
195 st.addMember(type, name); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
196 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
197 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
198 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
199 |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
200 DType typeOf(Identifier id, Scope sc) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
201 { |
77
13eea2c4e60d
Now able of --ast-dump-code with Pointer types and also codeGen int* x;
Anders Johnsen <skabet@gmail.com>
parents:
64
diff
changeset
|
202 if(auto i = cast(PointerIdentifier)id) |
83
9e90694f5da0
Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents:
82
diff
changeset
|
203 return (typeOf(i.pointerOf, sc)).getPointerTo(); |
136 | 204 else if(auto i = cast(StaticArrayIdentifier)id) |
205 return typeOf(i.arrayOf, sc).getAsStaticArray(i.size); | |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
144
diff
changeset
|
206 return sc.findType(id.get); |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
207 } |
101
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
208 |
129 | 209 Module current; |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
210 } |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
211 |
133
9c48871eb816
Now working with forward ref and structs in scope builder. New Symbol system should now be good to go!
Anders Johnsen <skabet@gmail.com>
parents:
132
diff
changeset
|
212 |
129 | 213 /** |
214 Add scopes to everything, and add all identifiers that correspond to types. | |
215 Types/Symbols are added by ForwardReference. | |
216 **/ | |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
217 class ScopeBuilder : Visitor!(void) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
218 { |
101
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
219 static ModuleHandler mHandle; |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
220 |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
221 static this() |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
222 { |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
223 mHandle = new ModuleHandler; |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
224 } |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
225 |
1 | 226 this() |
227 { | |
101
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
228 } |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
229 |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
230 override void visit(Module[] modules) |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
231 { |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
232 foreach(m ; modules) |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
233 visitModule(m); |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
234 |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
235 auto fr = new ForwardReference(); |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
236 |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
237 fr.visit(modules); |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
238 } |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
239 |
129 | 240 private void registerBasicTypeTo(char[] n, DType t, Scope sc, Module m) |
241 { | |
242 sc.types[n] = t; | |
243 auto id = new Identifier(n); | |
244 id.env = sc; | |
245 auto decl = new DummyDecl(); | |
136 | 246 auto sym = m.symbol.createMember(n, t, decl); |
247 sym.decl = decl; | |
129 | 248 decl.sym = sym; |
249 decl.env = sc; | |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
144
diff
changeset
|
250 sc.put(id.get, decl); |
129 | 251 } |
252 | |
101
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
253 override void visitModule(Module m) |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
254 { |
129 | 255 auto root = new Scope; |
256 table ~= root; | |
257 | |
258 m.symbol = new Symbol; | |
1 | 259 |
129 | 260 registerBasicTypeTo("void", DType.Void, root, m); |
261 registerBasicTypeTo("bool", DType.Bool, root, m); | |
262 registerBasicTypeTo("byte", DType.Byte, root, m); | |
263 registerBasicTypeTo("ubyte", DType.UByte, root, m); | |
264 registerBasicTypeTo("short", DType.Short, root, m); | |
265 registerBasicTypeTo("ushort", DType.UShort, root, m); | |
266 registerBasicTypeTo("int", DType.Int, root, m); | |
267 registerBasicTypeTo("uint", DType.UInt, root, m); | |
268 registerBasicTypeTo("long", DType.Long, root, m); | |
269 registerBasicTypeTo("ulong", DType.ULong, root, m); | |
270 | |
271 registerBasicTypeTo("char", DType.Char, root, m); | |
272 registerBasicTypeTo("wchar", DType.WChar, root, m); | |
273 registerBasicTypeTo("dchar", DType.DChar, root, m); | |
274 | |
275 registerBasicTypeTo("float", DType.Float, root, m); | |
276 registerBasicTypeTo("double", DType.Double, root, m); | |
277 registerBasicTypeTo("real", DType.Real, root, m); | |
119
c0b531362ca6
Non compileing commit. Work on floating points and casts
Anders Johnsen <skabet@gmail.com>
parents:
107
diff
changeset
|
278 |
99
857f0d530789
Imports and improved module statement
Anders Halager <halager@gmail.com>
parents:
94
diff
changeset
|
279 current().inModule = m; |
101
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
280 current().mHandle = mHandle; |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
281 mHandle.add(m); |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
282 m.env = current(); |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
283 super.visitModule(m); |
1 | 284 } |
285 | |
286 override void visitDecl(Decl d) | |
287 { | |
288 d.env = current(); | |
289 super.visitDecl(d); | |
290 } | |
291 | |
101
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
292 override void visitImportDecl(ImportDecl i) |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
293 { |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
294 i.env.imports ~= i; |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
295 super.visitImportDecl(i); |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
296 } |
fea8d61a2451
First step(the other first was a bad one) toward imports. You can now compile two files that use eachother - given that they both are in the command line. Right now it's only root sturcts and methods you can use(i guess...?)
Anders Johnsen <skabet@gmail.com>
parents:
99
diff
changeset
|
297 |
1 | 298 override void visitStmt(Stmt s) |
299 { | |
300 s.env = current(); | |
56
4ae365eff712
Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
Anders Johnsen <skabet@gmail.com>
parents:
55
diff
changeset
|
301 s.stmtIndex = s.env.stmtIndex; |
1 | 302 super.visitStmt(s); |
303 } | |
304 | |
305 override void visitExp(Exp e) | |
306 { | |
307 e.env = current(); | |
56
4ae365eff712
Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
Anders Johnsen <skabet@gmail.com>
parents:
55
diff
changeset
|
308 e.stmtIndex = e.env.stmtIndex; |
1 | 309 super.visitExp(e); |
310 } | |
311 | |
312 override void visitFuncDecl(FuncDecl d) | |
313 { | |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
144
diff
changeset
|
314 current().put(d.identifier.get, d); |
136 | 315 d.env = current(); |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
316 auto sc = push(); |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
317 |
63
9f8131676242
Now Decl's have a DType type(), and should use varType and returnType to get the old type id
Anders Halager <halager@gmail.com>
parents:
59
diff
changeset
|
318 visitExp(d.returnType); |
2
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
319 visitExp(d.identifier); |
56
4ae365eff712
Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
Anders Johnsen <skabet@gmail.com>
parents:
55
diff
changeset
|
320 sc.parentFunction = d; |
2
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
321 foreach (arg; d.funcArgs) |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
322 visitDecl(arg); |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
323 foreach (stmt; d.statements) |
56
4ae365eff712
Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
Anders Johnsen <skabet@gmail.com>
parents:
55
diff
changeset
|
324 { |
4ae365eff712
Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
Anders Johnsen <skabet@gmail.com>
parents:
55
diff
changeset
|
325 sc.currentStmtIndex++; |
2
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
326 visitStmt(stmt); |
56
4ae365eff712
Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
Anders Johnsen <skabet@gmail.com>
parents:
55
diff
changeset
|
327 } |
14 | 328 pop(sc); |
1 | 329 } |
330 | |
331 override void visitVarDecl(VarDecl d) | |
332 { | |
24 | 333 if (d.init) |
334 visitExp(d.init); | |
335 | |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
336 if (need_push > 0 && current().parentFunction !is null) { |
24 | 337 push(); |
338 --need_push; | |
339 } | |
340 | |
1 | 341 auto sc = current(); |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
144
diff
changeset
|
342 sc.put(d.identifier.get, d); |
24 | 343 d.env = sc; |
63
9f8131676242
Now Decl's have a DType type(), and should use varType and returnType to get the old type id
Anders Halager <halager@gmail.com>
parents:
59
diff
changeset
|
344 visitExp(d.varType); |
24 | 345 visitExp(d.identifier); |
1 | 346 } |
347 | |
22 | 348 override void visitStructDecl(StructDecl s) |
349 { | |
350 auto sc = current(); | |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
144
diff
changeset
|
351 sc.put(s.identifier.get, s); |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
53
diff
changeset
|
352 s.env = sc; |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
53
diff
changeset
|
353 auto type = new DStruct(s.identifier); |
129 | 354 |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
355 sc.types[s.identifier.get] = type; |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
53
diff
changeset
|
356 |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
53
diff
changeset
|
357 sc = push(); |
22 | 358 super.visitStructDecl(s); |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
53
diff
changeset
|
359 pop(sc); |
22 | 360 } |
361 | |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
362 override void visitClassDecl(ClassDecl s) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
363 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
364 auto sc = current(); |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
144
diff
changeset
|
365 sc.put(s.identifier.get, s); |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
366 s.env = sc; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
367 auto type = new DClass(s.identifier); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
368 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
369 sc.types[s.identifier.get] = type; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
370 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
371 sc = push(); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
372 super.visitClassDecl(s); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
373 pop(sc); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
374 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
375 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
376 override void visitInterfaceDecl(InterfaceDecl s) |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
377 { |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
378 auto sc = current(); |
145
a14ac9e5c858
Changes Scope to use char[]'s insted of Identifiers for lookup.
Anders Johnsen <skabet@gmail.com>
parents:
144
diff
changeset
|
379 sc.put(s.identifier.get, s); |
144
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
380 s.env = sc; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
381 auto type = new DInterface(s.identifier); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
382 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
383 sc.types[s.identifier.get] = type; |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
384 |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
385 sc = push(); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
386 super.visitInterfaceDecl(s); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
387 pop(sc); |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
388 } |
6e6355fb5f0f
- Parsing nested attributes.
Anders Johnsen <skabet@gmail.com>
parents:
136
diff
changeset
|
389 |
14 | 390 override void visitDeclStmt(DeclStmt d) |
391 { | |
24 | 392 ++need_push; |
14 | 393 super.visitDeclStmt(d); |
394 } | |
24 | 395 private uint need_push = 0; |
14 | 396 |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
397 override void visitIfStmt(IfStmt s) |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
398 { |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
399 s.env = current(); |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
400 visitExp(s.cond); |
14 | 401 auto sc = push(); |
45 | 402 visitStmt(s.then_body); |
14 | 403 pop(sc); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
404 |
45 | 405 if (s.else_body !is null) |
406 { | |
407 sc = push(); | |
408 visitStmt(s.else_body); | |
409 pop(sc); | |
410 } | |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
411 } |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
412 |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
413 override void visitWhileStmt(WhileStmt s) |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
414 { |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
415 s.env = current(); |
14 | 416 auto sc = push(); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
417 super.visitWhileStmt(s); |
14 | 418 pop(sc); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
419 } |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
420 |
149
393a1f47a6d2
For loops in AST and sema. Should have correct scope and such now.
Anders Johnsen <skabet@gmail.com>
parents:
145
diff
changeset
|
421 override void visitForStmt(ForStmt s) |
393a1f47a6d2
For loops in AST and sema. Should have correct scope and such now.
Anders Johnsen <skabet@gmail.com>
parents:
145
diff
changeset
|
422 { |
393a1f47a6d2
For loops in AST and sema. Should have correct scope and such now.
Anders Johnsen <skabet@gmail.com>
parents:
145
diff
changeset
|
423 s.env = current(); |
393a1f47a6d2
For loops in AST and sema. Should have correct scope and such now.
Anders Johnsen <skabet@gmail.com>
parents:
145
diff
changeset
|
424 auto sc = push(); |
393a1f47a6d2
For loops in AST and sema. Should have correct scope and such now.
Anders Johnsen <skabet@gmail.com>
parents:
145
diff
changeset
|
425 super.visitForStmt(s); |
393a1f47a6d2
For loops in AST and sema. Should have correct scope and such now.
Anders Johnsen <skabet@gmail.com>
parents:
145
diff
changeset
|
426 pop(sc); |
393a1f47a6d2
For loops in AST and sema. Should have correct scope and such now.
Anders Johnsen <skabet@gmail.com>
parents:
145
diff
changeset
|
427 } |
393a1f47a6d2
For loops in AST and sema. Should have correct scope and such now.
Anders Johnsen <skabet@gmail.com>
parents:
145
diff
changeset
|
428 |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
429 override void visitCompoundStmt(CompoundStatement s) |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
430 { |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
431 s.env = current(); |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
432 auto sc = push(); |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
433 super.visitCompoundStmt(s); |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
434 pop(sc); |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
435 } |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
436 |
1 | 437 private: |
438 Scope[] table; | |
439 | |
14 | 440 Scope push() |
1 | 441 { |
14 | 442 auto sc = new Scope(current()); |
443 table ~= sc; | |
444 return sc; | |
1 | 445 } |
446 | |
14 | 447 Scope pop(Scope sc = null) |
1 | 448 { |
14 | 449 if (sc !is null) |
450 { | |
451 table.length = table.find(sc); | |
452 return sc; | |
453 } | |
454 | |
1 | 455 auto res = table[$ - 1]; |
456 table.length = table.length - 1; | |
457 return res; | |
458 } | |
459 | |
460 Scope current() | |
461 { | |
462 return table[$ - 1]; | |
463 } | |
464 } | |
465 |