annotate sema/ScopeBuilder.d @ 136:2be29b296081

Lots of changes: - Parsing classes and interfaces - Fixed some seg faults in sema - Supporting "private" to some extend - And a lot of other small fixes
author johnsen@johnsen-laptop
date Fri, 11 Jul 2008 21:47:57 +0200
parents 9c48871eb816
children 6e6355fb5f0f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
2
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
3 import tango.io.Stdout,
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
4 tango.core.Array : find;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
5
26
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents: 24
diff changeset
6 public
129
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
7 import sema.Scope,
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
8 sema.Symbol;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
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
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
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
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
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
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
19 inFunctionBodyStack.push(false);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
20 foreach (mod; modules)
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
21 {
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
22 current = mod;
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
23 foreach (decl; mod.decls)
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
24 visitDecl(decl);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
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
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
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
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
35
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
36 inFunctionBodyStack.push(true);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
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
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
41 inFunctionBodyStack.pop();
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
42
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
43 d.sym = current.symbol.createMember(
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
44 d.identifier.get,
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
45 d.type,
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
46 d.env.find(d.identifier));
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
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
57 DType t = typeOf(d.varType, d.env);
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
58 d.sym = current.symbol.createAlias(
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
59 d.identifier.get,
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
60 d.env.find(d.varType).sym,
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
61 d.env.find(d.identifier));
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
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
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
67 auto old = current.symbol;
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
68 current.symbol = s.sym;
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
69 inFunctionBodyStack.push(false);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
70 super.visitStructDecl(s);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
71 inFunctionBodyStack.pop();
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
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
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
75 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
76 {
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
77 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
78 return (typeOf(i.pointerOf, sc)).getPointerTo();
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
79 else if(auto i = cast(StaticArrayIdentifier)id)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
80 return typeOf(i.arrayOf, sc).getAsStaticArray(i.size);
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
81 return sc.findType(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
82 }
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
83
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
84 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
85 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
86 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
87 }
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
88
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
89 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
90 {
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
91 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
92 {
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
93 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
94 {
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 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
96 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
97 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
98 }
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
99 }
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
100
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
101 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
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 auto st = s.env.findType(s.identifier).asStruct;
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
104 s.sym = current.symbol.createMember(
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
105 s.identifier.get,
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
106 st,
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
107 s.env.find(s.identifier));
55
79cb0afafabe Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents: 53
diff changeset
108
107
189c049cbfcc Cleanup of codegen, better support for operators a few bugfixes
Anders Halager <halager@gmail.com>
parents: 103
diff changeset
109 foreach (decl; s.decls)
129
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
110 {
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
111 DType type;
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
112 char[] name;
107
189c049cbfcc Cleanup of codegen, better support for operators a few bugfixes
Anders Halager <halager@gmail.com>
parents: 103
diff changeset
113 if (auto varDecl = cast(VarDecl)decl)
129
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
114 {
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
115 type = typeOf(varDecl.varType, varDecl.env);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
116 name = varDecl.identifier.get;
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
117 }
107
189c049cbfcc Cleanup of codegen, better support for operators a few bugfixes
Anders Halager <halager@gmail.com>
parents: 103
diff changeset
118 else if (auto fd = cast(FuncDecl)decl)
129
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
119 {
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
120 type = fd.type;
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
121 name = fd.identifier.get;
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
122 }
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
123 st.addMember(type, name);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
124 }
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
125 }
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
126
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
127 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
128 {
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
129 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
130 return (typeOf(i.pointerOf, sc)).getPointerTo();
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
131 else if(auto i = cast(StaticArrayIdentifier)id)
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
132 return typeOf(i.arrayOf, sc).getAsStaticArray(i.size);
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
133 return sc.findType(id);
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
134 }
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
135
129
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
136 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
137 }
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
138
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
139
129
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
140 /**
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
141 Add scopes to everything, and add all identifiers that correspond to types.
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
142 Types/Symbols are added by ForwardReference.
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
143 **/
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
144 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
145 {
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
146 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
147
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
148 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
149 {
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
150 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
151 }
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
152
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
153 this()
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
154 {
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
155 }
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
156
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
157 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
158 {
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
159 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
160 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
161
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
162 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
163
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
164 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
165 }
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
166
129
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
167 private void registerBasicTypeTo(char[] n, DType t, Scope sc, Module m)
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
168 {
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
169 sc.types[n] = t;
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
170 auto id = new Identifier(n);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
171 id.env = sc;
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
172 auto decl = new DummyDecl();
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
173 auto sym = m.symbol.createMember(n, t, decl);
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
174 sym.decl = decl;
129
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
175 decl.sym = sym;
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
176 decl.env = sc;
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
177 sc.put(id, decl);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
178 }
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
179
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
180 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
181 {
129
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
182 auto root = new Scope;
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
183 table ~= root;
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
184
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
185 m.symbol = new Symbol;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
186
129
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
187 registerBasicTypeTo("void", DType.Void, root, m);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
188 registerBasicTypeTo("bool", DType.Bool, root, m);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
189 registerBasicTypeTo("byte", DType.Byte, root, m);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
190 registerBasicTypeTo("ubyte", DType.UByte, root, m);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
191 registerBasicTypeTo("short", DType.Short, root, m);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
192 registerBasicTypeTo("ushort", DType.UShort, root, m);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
193 registerBasicTypeTo("int", DType.Int, root, m);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
194 registerBasicTypeTo("uint", DType.UInt, root, m);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
195 registerBasicTypeTo("long", DType.Long, root, m);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
196 registerBasicTypeTo("ulong", DType.ULong, root, m);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
197
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
198 registerBasicTypeTo("char", DType.Char, root, m);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
199 registerBasicTypeTo("wchar", DType.WChar, root, m);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
200 registerBasicTypeTo("dchar", DType.DChar, root, m);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
201
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
202 registerBasicTypeTo("float", DType.Float, root, m);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
203 registerBasicTypeTo("double", DType.Double, root, m);
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
204 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
205
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
206 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
207 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
208 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
209 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
210 super.visitModule(m);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
211 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
212
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
213 override void visitDecl(Decl d)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
214 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
215 d.env = current();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
216 super.visitDecl(d);
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
217 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
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 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
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 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
222 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
223 }
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
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
225 override void visitStmt(Stmt s)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
226 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
227 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
228 s.stmtIndex = s.env.stmtIndex;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
229 super.visitStmt(s);
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
230 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
231
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
232 override void visitExp(Exp e)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
233 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
234 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
235 e.stmtIndex = e.env.stmtIndex;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
236 super.visitExp(e);
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
237 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
238
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
239 override void visitFuncDecl(FuncDecl d)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
240 {
129
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
241 current().put(d.identifier, d);
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
242 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
243 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
244
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
245 visitExp(d.returnType);
2
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
246 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
247 sc.parentFunction = d;
2
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
248 foreach (arg; d.funcArgs)
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
249 visitDecl(arg);
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
250 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
251 {
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
252 sc.currentStmtIndex++;
2
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
253 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
254 }
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
255 pop(sc);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
256 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
257
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
258 override void visitVarDecl(VarDecl d)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
259 {
24
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
260 if (d.init)
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
261 visitExp(d.init);
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
262
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
263 if (need_push > 0 && current().parentFunction !is null) {
24
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
264 push();
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
265 --need_push;
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
266 }
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
267
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
268 auto sc = current();
129
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
269 sc.put(d.identifier, d);
24
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
270 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
271 visitExp(d.varType);
24
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
272 visitExp(d.identifier);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
273 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
274
22
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
275 override void visitStructDecl(StructDecl s)
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
276 {
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
277 auto sc = current();
129
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
278 sc.put(s.identifier, s);
55
79cb0afafabe Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents: 53
diff changeset
279 s.env = sc;
79cb0afafabe Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents: 53
diff changeset
280 auto type = new DStruct(s.identifier);
129
ed815b31479b Added a Symbol
Anders Halager <halager@gmail.com>
parents: 119
diff changeset
281
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
282 sc.types[s.identifier.get] = type;
55
79cb0afafabe Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents: 53
diff changeset
283
79cb0afafabe Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents: 53
diff changeset
284 sc = push();
136
2be29b296081 Lots of changes:
johnsen@johnsen-laptop
parents: 133
diff changeset
285 Stdout(sc).newline;
22
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
286 super.visitStructDecl(s);
55
79cb0afafabe Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents: 53
diff changeset
287 pop(sc);
22
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
288 }
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
289
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
290 override void visitDeclStmt(DeclStmt d)
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
291 {
24
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
292 ++need_push;
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
293 super.visitDeclStmt(d);
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
294 }
24
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
295 private uint need_push = 0;
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
296
11
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
297 override void visitIfStmt(IfStmt s)
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
298 {
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
299 s.env = current();
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
300 visitExp(s.cond);
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
301 auto sc = push();
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
302 visitStmt(s.then_body);
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
303 pop(sc);
11
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
304
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
305 if (s.else_body !is null)
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
306 {
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
307 sc = push();
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
308 visitStmt(s.else_body);
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
309 pop(sc);
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
310 }
11
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
311 }
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
312
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
313 override void visitWhileStmt(WhileStmt s)
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
314 {
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
315 s.env = current();
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
316 auto sc = push();
11
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
317 super.visitWhileStmt(s);
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
318 pop(sc);
11
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
319 }
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
320
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
321 override void visitCompoundStmt(CompoundStatement s)
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
322 {
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
323 s.env = current();
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
324 auto sc = push();
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
325 super.visitCompoundStmt(s);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
326 pop(sc);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
327 }
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
328
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
329 private:
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
330 Scope[] table;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
331
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
332 Scope push()
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
333 {
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
334 auto sc = new Scope(current());
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
335 table ~= sc;
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
336 return sc;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
337 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
338
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
339 Scope pop(Scope sc = null)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
340 {
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
341 if (sc !is null)
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
342 {
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
343 table.length = table.find(sc);
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
344 return sc;
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
345 }
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
346
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
347 auto res = table[$ - 1];
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
348 table.length = table.length - 1;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
349 return res;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
350 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
351
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
352 Scope current()
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
353 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
354 return table[$ - 1];
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
355 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
356 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
357