annotate sema/Scope.d @ 99:857f0d530789 new_gen

Imports and improved module statement Allow "module a.b.c" Supports most forms of D's import. import A, B; import A, B = C; import A, B : a = b, c;
author Anders Halager <halager@gmail.com>
date Tue, 06 May 2008 21:59:22 +0200
parents 621cedba53ea
children fea8d61a2451
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: 82
diff changeset
1 module sema.Scope;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
2
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
3 import tango.io.Stdout;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
4
26
b4dc2b2c0e38 Added a DType class
Anders Halager <halager@gmail.com>
parents: 24
diff changeset
5 import lexer.Token,
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 93
diff changeset
6 ast.Module,
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: 53
diff changeset
7 ast.Decl,
27
9031487e97d7 Various changes related to DType
Anders Halager <halager@gmail.com>
parents: 26
diff changeset
8 ast.Exp;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
9
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
10 public
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
11 import sema.DType;
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
12
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
13 class Scope
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
14 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
15 this() {}
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
16 this(Scope enclosing)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
17 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
18 this.enclosing = enclosing;
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
19 this.func = enclosing.func;
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 93
diff changeset
20 this.inModule = enclosing.inModule;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
21 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
22
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
23 Scope enclosing;
99
857f0d530789 Imports and improved module statement
Anders Halager <halager@gmail.com>
parents: 93
diff changeset
24 Module inModule;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
25
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
26 void add(Identifier id)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
27 {
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
28 symbols[id] = id;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
29 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
30
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
31 Identifier find(Identifier id)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
32 {
82
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 59
diff changeset
33 if(!id)
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 59
diff changeset
34 return null;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
35 if (auto sym = id in symbols)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
36 return *sym;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
37 if (enclosing !is null)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
38 return enclosing.find(id);
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
39 return null;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
40 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
41
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
42 DType findType(Identifier id)
22
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
43 {
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
44 if (auto type = id.get in types)
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
45 return *type;
22
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
46 if (enclosing !is null)
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
47 return enclosing.findType(id);
22
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
48 return null;
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
49 }
e331e4e816e4 now handling structs to some extend
johnsen@johnsen-laptop
parents: 14
diff changeset
50
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
51 char[][] names()
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
52 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
53 char[][] res;
59
1d6f4ad38a91 Make most of the tests pass again
Anders Halager <halager@gmail.com>
parents: 56
diff changeset
54 if (parentFunction() !is null)
1d6f4ad38a91 Make most of the tests pass again
Anders Halager <halager@gmail.com>
parents: 56
diff changeset
55 res ~= "pf: " ~ parentFunction().identifier.get;
24
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
56 if (enclosing)
2d28b21faad6 New codegen!
Anders Halager <halager@gmail.com>
parents: 22
diff changeset
57 res = enclosing.names;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
58 foreach (id, sym; symbols)
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
59 res ~= sym.name ~ " : " ~ (sym.type is null? "?" : sym.type.name);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
60 return res;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
61 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
62
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: 53
diff changeset
63 FuncDecl parentFunction()
2
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
64 {
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
65 if (func !is null)
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
66 return func;
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
67 else if (enclosing !is null)
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
68 return enclosing.parentFunction();
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
69 else
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
70 return null;
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
71 }
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
72
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: 53
diff changeset
73 int stmtIndex()
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: 53
diff changeset
74 {
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: 53
diff changeset
75 if (currentStmtIndex != -1)
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: 53
diff changeset
76 return currentStmtIndex;
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: 53
diff changeset
77 else if (enclosing !is null)
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: 53
diff changeset
78 return enclosing.stmtIndex();
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: 53
diff changeset
79 else
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: 53
diff changeset
80 return -1;
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: 53
diff changeset
81 }
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: 53
diff changeset
82
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
83 int opEquals(Object o)
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
84 {
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
85 return this is o;
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
86 }
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
87
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
88 char[] toString()
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
89 {
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
90 if (func)
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: 53
diff changeset
91 return Stdout.layout.convert("{}: {}", func.identifier.get, symbols.length);
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: 28
diff changeset
92 return Stdout.layout.convert("root: {}", symbols.length);
14
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
93 }
a51bdf15a33d Better scopes.
Anders Halager <halager@gmail.com>
parents: 2
diff changeset
94
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: 53
diff changeset
95 FuncDecl parentFunction(FuncDecl f)
2
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
96 {
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
97 func = f;
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
98 return f;
ae5bbe4e7fd6 Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
99 }
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
100 DType[char[]] types;
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: 53
diff changeset
101 int currentStmtIndex = -1;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
102 private:
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
103 Identifier[Identifier] symbols;
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: 53
diff changeset
104 FuncDecl func;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
105 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
106