annotate sema/ScopeBuilder.d @ 107:189c049cbfcc new_gen

Cleanup of codegen, better support for operators a few bugfixes
author Anders Halager <halager@gmail.com>
date Sun, 25 May 2008 14:40:14 +0200
parents 09b4d74cb3f5
children c0b531362ca6
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
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
7 import sema.Scope;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
8
59
1d6f4ad38a91 Make most of the tests pass again
Anders Halager <halager@gmail.com>
parents: 56
diff changeset
9 import sema.Visitor,
1d6f4ad38a91 Make most of the tests pass again
Anders Halager <halager@gmail.com>
parents: 56
diff changeset
10 basic.SmallArray;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
11
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
12 class ForwardReference : Visitor!(void)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
13 {
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
14 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
15 {
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 this.modules = 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
17 super.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
18 }
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
19
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
20 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
21 {
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
22 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
23 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
24 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
25 visitDecl(arg);
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
26 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
27 visitStmt(stmt);
59
1d6f4ad38a91 Make most of the tests pass again
Anders Halager <halager@gmail.com>
parents: 56
diff changeset
28
93
621cedba53ea Removed the Symbol from semantics - it was not needed anymore. From now on you set the type by doing a setType on an Identifier.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
29 d.env.find(d.identifier).setType(d.type);
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
30 }
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
31
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 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
33 {
82
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
34 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
35 visitExp(d.identifier);
82
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
36
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
37 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
38 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
39
93
621cedba53ea Removed the Symbol from semantics - it was not needed anymore. From now on you set the type by doing a setType on an Identifier.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
40 d.env.find(d.identifier).setType( typeOf(d.varType, d.env) );
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
41 }
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
42
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
43 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
44 {
107
189c049cbfcc Cleanup of codegen, better support for operators a few bugfixes
Anders Halager <halager@gmail.com>
parents: 103
diff changeset
45 super.visitStructDecl(s);
189c049cbfcc Cleanup of codegen, better support for operators a few bugfixes
Anders Halager <halager@gmail.com>
parents: 103
diff changeset
46
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 DType[char[]] types;
55
79cb0afafabe Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents: 53
diff changeset
48
64
91f10c34cd7b Fixed some bugs, removed the function gathering pass in codegen and types are
Anders Halager <halager@gmail.com>
parents: 63
diff changeset
49 auto st = s.env.types[s.identifier.get].asStruct;
107
189c049cbfcc Cleanup of codegen, better support for operators a few bugfixes
Anders Halager <halager@gmail.com>
parents: 103
diff changeset
50 foreach (decl; s.decls)
189c049cbfcc Cleanup of codegen, better support for operators a few bugfixes
Anders Halager <halager@gmail.com>
parents: 103
diff changeset
51 if (auto varDecl = cast(VarDecl)decl)
102
cd066f3b539a Parsing methods in structs - error on semantics though.
Anders Johnsen <skabet@gmail.com>
parents: 101
diff changeset
52 st.addMember(typeOf(varDecl.varType, varDecl.env), varDecl.identifier.get);
107
189c049cbfcc Cleanup of codegen, better support for operators a few bugfixes
Anders Halager <halager@gmail.com>
parents: 103
diff changeset
53 else if (auto fd = cast(FuncDecl)decl)
189c049cbfcc Cleanup of codegen, better support for operators a few bugfixes
Anders Halager <halager@gmail.com>
parents: 103
diff changeset
54 st.addMember(fd.type, fd.identifier.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
55 }
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
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
57 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
58 {
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
59 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
60 return (typeOf(i.pointerOf, sc)).getPointerTo();
81
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 77
diff changeset
61 if(auto i = cast(ArrayIdentifier)id)
83
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
62 return typeOf(i.arrayOf, sc).getAsArray(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
63 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
64 }
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
65
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
66 Module[] modules;
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
67 }
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
68
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
69 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
70 {
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
71 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
72
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
73 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
74 {
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
75 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
76 }
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
77
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
78 this()
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
79 {
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
80 }
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
81
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
82 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
83 {
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
84 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
85 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
86
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
87 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
88
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
89 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
90 }
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
91
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
92 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
93 {
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
94 table ~= new Scope;
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
95 table[table.length-1].types["void"] = DType.Void;
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
96 table[table.length-1].types["bool"] = DType.Bool;
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
97 table[table.length-1].types["byte"] = DType.Byte;
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
98 table[table.length-1].types["ubyte"] = DType.UByte;
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
99 table[table.length-1].types["short"] = DType.Short;
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
100 table[table.length-1].types["ushort"] = DType.UShort;
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
101 table[table.length-1].types["int"] = DType.Int;
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
102 table[table.length-1].types["uint"] = DType.UInt;
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
103 table[table.length-1].types["long"] = DType.Long;
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
104 table[table.length-1].types["ulong"] = DType.ULong;
103
09b4d74cb3f5 Parsing char, wchar and dchar as basic types.
Anders Johnsen <skabet@gmail.com>
parents: 102
diff changeset
105 table[table.length-1].types["char"] = DType.Char;
09b4d74cb3f5 Parsing char, wchar and dchar as basic types.
Anders Johnsen <skabet@gmail.com>
parents: 102
diff changeset
106 table[table.length-1].types["wchar"] = DType.WChar;
09b4d74cb3f5 Parsing char, wchar and dchar as basic types.
Anders Johnsen <skabet@gmail.com>
parents: 102
diff changeset
107 table[table.length-1].types["dchar"] = DType.DChar;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
108
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 94
diff changeset
109 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
110 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
111 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
112 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
113 super.visitModule(m);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
114 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
115
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
116 override void visitDecl(Decl d)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
117 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
118 d.env = current();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
119 super.visitDecl(d);
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
120 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
121
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
122 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
123 {
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
124 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
125 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
126 }
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
127
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
128 override void visitStmt(Stmt s)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
129 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
130 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
131 s.stmtIndex = s.env.stmtIndex;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
132 super.visitStmt(s);
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
133 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
134
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
135 override void visitExp(Exp e)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
136 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
137 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
138 e.stmtIndex = e.env.stmtIndex;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
139 super.visitExp(e);
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
140 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
141
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
142 override void visitFuncDecl(FuncDecl d)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
143 {
93
621cedba53ea Removed the Symbol from semantics - it was not needed anymore. From now on you set the type by doing a setType on an Identifier.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
144 current().add(d.identifier);
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
145 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
146
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
147 visitExp(d.returnType);
2
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
148 visitExp(d.identifier);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
149 d.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
150 sc.parentFunction = d;
2
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
151 foreach (arg; d.funcArgs)
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
152 visitDecl(arg);
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
153 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
154 {
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
155 sc.currentStmtIndex++;
2
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
156 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
157 }
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
158 pop(sc);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
159 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
160
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
161 override void visitVarDecl(VarDecl d)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
162 {
24
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
163 if (d.init)
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
164 visitExp(d.init);
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
165
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
166 if (need_push > 0 && current().parentFunction !is null) {
24
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
167 push();
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
168 --need_push;
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
169 }
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
170
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
171 auto sc = current();
93
621cedba53ea Removed the Symbol from semantics - it was not needed anymore. From now on you set the type by doing a setType on an Identifier.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
172 sc.add(d.identifier);
24
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
173 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
174 visitExp(d.varType);
24
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
175 visitExp(d.identifier);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
176 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
177
22
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
178 override void visitStructDecl(StructDecl s)
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
179 {
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
180 auto sc = current();
93
621cedba53ea Removed the Symbol from semantics - it was not needed anymore. From now on you set the type by doing a setType on an Identifier.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
181 sc.add(s.identifier);
55
79cb0afafabe Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents: 53
diff changeset
182 s.env = sc;
79cb0afafabe Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents: 53
diff changeset
183 auto type = new DStruct(s.identifier);
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
184
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
185 sc.types[s.identifier.get] = type;
55
79cb0afafabe Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents: 53
diff changeset
186
79cb0afafabe Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents: 53
diff changeset
187 sc = push();
22
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
188 super.visitStructDecl(s);
55
79cb0afafabe Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents: 53
diff changeset
189 pop(sc);
22
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
190 }
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
191
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
192 override void visitDeclStmt(DeclStmt d)
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
193 {
24
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
194 ++need_push;
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
195 super.visitDeclStmt(d);
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
196 }
24
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
197 private uint need_push = 0;
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
198
11
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
199 override void visitIfStmt(IfStmt s)
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
200 {
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
201 s.env = current();
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
202 visitExp(s.cond);
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
203 auto sc = push();
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
204 visitStmt(s.then_body);
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
205 pop(sc);
11
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
206
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
207 if (s.else_body !is null)
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
208 {
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
209 sc = push();
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
210 visitStmt(s.else_body);
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
211 pop(sc);
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
212 }
11
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
213 }
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
214
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
215 override void visitWhileStmt(WhileStmt s)
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
216 {
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
217 s.env = current();
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
218 auto sc = push();
11
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
219 super.visitWhileStmt(s);
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
220 pop(sc);
11
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
221 }
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
222
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
223 override void visitCompoundStmt(CompoundStatement s)
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
224 {
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
225 s.env = current();
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
226 auto sc = push();
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
227 super.visitCompoundStmt(s);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
228 pop(sc);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
229 }
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
230
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
231 private:
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
232 Scope[] table;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
233
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
234 Scope push()
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
235 {
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
236 auto sc = new Scope(current());
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
237 table ~= sc;
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
238 return sc;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
239 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
240
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
241 Scope pop(Scope sc = null)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
242 {
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
243 if (sc !is null)
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
244 {
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
245 table.length = table.find(sc);
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
246 return sc;
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
247 }
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 11
diff changeset
248
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
249 auto res = table[$ - 1];
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
250 table.length = table.length - 1;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
251 return res;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
252 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
253
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
254 Scope current()
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
255 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
256 return table[$ - 1];
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
257 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
258 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
259