annotate parser/Parser.d @ 94:48bb2287c035 new_gen

Added Modules. Right now it's very simple - will grow with time and need.
author Anders Johnsen <skabet@gmail.com>
date Tue, 06 May 2008 16:24:14 +0200
parents 771ac63898e2
children 857f0d530789
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
1 module parser.Parser;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
2
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
3 import lexer.Lexer,
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
4 lexer.Token;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
5
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
6 import parser.Action;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
7
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
8 import basic.Message;
21
0fb2d13dce37 Now working with gdc also (gdc use reverse paremeter validating on function calls)
johnsen@johnsen-laptop
parents: 12
diff changeset
9
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
10 import basic.SmallArray,
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
11 basic.SourceManager;
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
12
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
13 import tango.io.Stdout,
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
14 Integer = tango.text.convert.Integer;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
15
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
16 class Parser
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
17 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
18 Action action;
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
19 MessageHandler messages;
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
20 alias Object Exp;
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
21 alias Object Stmt;
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
22 alias Object Decl;
94
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
23 alias Object Module;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
24
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
25 public:
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
26
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
27 this(MessageHandler messages)
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
28 {
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
29 this.messages = messages;
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
30 }
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
31
94
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
32 Module parse(SourceManager sm, Lexer lexer, Action act)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
33 {
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
34 this.sm = sm;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
35 this.lexer = lexer;
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
36 this.action = act;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
37
94
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
38 auto m = parseModule();
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
39
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
40 while(lexer.peek.type != Tok.EOF)
94
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
41 action.actOnModuleDecl(m, parseDecl());
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
42
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
43 return m;
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
44 }
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
45
94
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
46 Module parseModule()
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
47 {
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
48 if(lexer.peek.type == Tok.Module)
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
49 {
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
50 lexer.next;
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
51 if(lexer.peek.type != Tok.Identifier)
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
52 messages.report(UnexpectedTok, lexer.peek.location)
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
53 .arg(lexer.peek.type)
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
54 .arg(Tok.Identifier)
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
55 .fatal(ExitLevel.Parser);
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
56
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
57 auto m = action.actOnModule(sm.getText(lexer.next.asRange));
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
58 require(Tok.Seperator);
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
59 return m;
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
60 }
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
61
48bb2287c035 Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents: 92
diff changeset
62 return action.actOnModule(sm.getFile(lexer.peek.location));
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
63 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
64
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
65 Decl parseDecl()
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
66 {
28
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
67 Token t = lexer.peek;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
68
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
69 if (t.isBasicType || t.isIdentifier)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
70 {
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: 89
diff changeset
71 Id type;
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: 89
diff changeset
72 Id iden;
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: 89
diff changeset
73 int len = peekParseType;
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: 89
diff changeset
74 if(lexer.peek(len).type == Tok.Identifier && len != 0)
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
75 {
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: 89
diff changeset
76 type = parseType;
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: 89
diff changeset
77 parseDeclAfterInvalidType:
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: 89
diff changeset
78 iden = Id(require(Tok.Identifier));
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: 89
diff changeset
79 Token next = lexer.peek();
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: 89
diff changeset
80 if (next.type == Tok.Seperator)
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: 89
diff changeset
81 {
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: 89
diff changeset
82 Token sep = lexer.next();
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: 89
diff changeset
83 return action.actOnDeclarator(type, iden, null);
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: 89
diff changeset
84 }
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: 89
diff changeset
85 else if (next.type == Tok.Assign)
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: 89
diff changeset
86 {
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: 89
diff changeset
87 Token assign = lexer.next();
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: 89
diff changeset
88 Exp exp = parseExpression();
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: 89
diff changeset
89 require(Tok.Seperator);
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: 89
diff changeset
90 return action.actOnDeclarator(type, iden, exp);
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: 89
diff changeset
91 }
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: 89
diff changeset
92 else if (next.type == Tok.OpenParentheses)
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: 89
diff changeset
93 return parseFunc(type, iden);
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: 89
diff changeset
94 else
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: 89
diff changeset
95 messages.report(UnexpectedTok, next.location).arg(next.getType);
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
96 }
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: 89
diff changeset
97 t = lexer.peek(len);
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: 89
diff changeset
98 messages.report(InvalidDeclType, t.location)
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: 89
diff changeset
99 .arg(sm.getText(t.asRange));
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: 89
diff changeset
100 while(len--)
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: 89
diff changeset
101 lexer.next;
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: 89
diff changeset
102 while(lexer.peek.type != Tok.Identifier)
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: 89
diff changeset
103 lexer.next;
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: 89
diff changeset
104 type = Id(lexer.peek);
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: 89
diff changeset
105 goto parseDeclAfterInvalidType;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
106 }
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
107 else if (t.type == Tok.Struct)
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
108 {
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: 51
diff changeset
109 Id type = Id(lexer.next);
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: 51
diff changeset
110 Id iden = Id(require(Tok.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: 51
diff changeset
111
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: 51
diff changeset
112 return parseStruct(type, iden);
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
113 }
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: 89
diff changeset
114 messages.report(UnexpectedTok, t.location)
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: 89
diff changeset
115 .arg(t.getType)
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: 89
diff changeset
116 .arg(Tok.Identifier)
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: 89
diff changeset
117 .fatal(ExitLevel.Parser);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
118 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
119
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
120 /**
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: 51
diff changeset
121 Parse struct
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: 51
diff changeset
122 */
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: 51
diff changeset
123 Decl parseStruct(Id type, Id iden)
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: 51
diff changeset
124 {
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: 51
diff changeset
125 auto decl = action.actOnDeclarator(type, iden, null);
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: 51
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: 51
diff changeset
127 require(Tok.OpenBrace);
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: 51
diff changeset
128
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: 51
diff changeset
129 while(lexer.peek.isBasicType || lexer.peek.isIdentifier)
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: 51
diff changeset
130 {
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: 51
diff changeset
131 Id var_type = Id(lexer.next);
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: 51
diff changeset
132 Id var_iden = Id(require(Tok.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: 51
diff changeset
133 Token next = lexer.peek();
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: 51
diff changeset
134 if (next.type == Tok.Seperator)
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: 51
diff changeset
135 {
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: 51
diff changeset
136 Token sep = lexer.next();
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: 51
diff changeset
137 action.actOnStructMember(decl, var_type, var_iden, null);
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: 51
diff changeset
138 continue;
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: 51
diff changeset
139 }
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: 51
diff changeset
140 else if (next.type == Tok.Assign)
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: 51
diff changeset
141 {
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: 51
diff changeset
142 Token assign = lexer.next();
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: 51
diff changeset
143 Exp exp = parseExpression();
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: 51
diff changeset
144 require(Tok.Seperator);
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: 51
diff changeset
145 action.actOnStructMember(decl, var_type, var_iden, exp);
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: 51
diff changeset
146 continue;
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: 51
diff changeset
147 }
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
148 messages.report(UnexpectedTok, next.location).arg(next.getType);
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: 51
diff changeset
149 }
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: 51
diff changeset
150
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: 51
diff changeset
151 require(Tok.CloseBrace);
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: 51
diff changeset
152
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: 51
diff changeset
153 return decl;
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: 51
diff changeset
154 }
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: 51
diff changeset
155 /**
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
156 Parse statements.
28
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
157
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
158 This is the place to attack!
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
159 */
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
160 Stmt parseStatement()
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
161 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
162 Token t = lexer.peek;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
163
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
164 switch(t.type)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
165 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
166 case Tok.Return:
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
167 Token ret = lexer.next;
46
90fb4fdfefdd While is back
Anders Halager <halager@gmail.com>
parents: 45
diff changeset
168 Exp exp;
90fb4fdfefdd While is back
Anders Halager <halager@gmail.com>
parents: 45
diff changeset
169 if (lexer.peek.type != Tok.Seperator)
90fb4fdfefdd While is back
Anders Halager <halager@gmail.com>
parents: 45
diff changeset
170 exp = parseExpression();
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
171 require(Tok.Seperator);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
172 return action.actOnReturnStmt(ret, exp);
5
2c5a8f4c254a Added very simple if support.
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
173
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
174 /*
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
175 if (cond)
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
176 single statement | compound statement
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
177 [else
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
178 single statement | compound statement]
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
179 */
5
2c5a8f4c254a Added very simple if support.
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
180 case Tok.If:
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
181 Token _if = lexer.next();
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
182
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
183 require(Tok.OpenParentheses);
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
184 Exp cond = parseExpression();
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
185 require(Tok.CloseParentheses);
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
186
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
187 Stmt thenB = parseSingleOrCompoundStatement();
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
188
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
189 // if there is no else part we use the if as token, to have
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
190 // something than can be passed along
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
191 Token _else = _if;
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
192 Stmt elseB;
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
193 if (lexer.peek.type == Tok.Else)
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
194 {
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
195 _else = lexer.next;
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
196 elseB = parseSingleOrCompoundStatement();
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
197 }
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
198
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
199 return action.actOnIfStmt(_if, cond, thenB, _else, elseB);
5
2c5a8f4c254a Added very simple if support.
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
200
47
b0a691727a0c Stricter while - the () was optional before
Anders Halager <halager@gmail.com>
parents: 46
diff changeset
201 /*
b0a691727a0c Stricter while - the () was optional before
Anders Halager <halager@gmail.com>
parents: 46
diff changeset
202 while (cond)
b0a691727a0c Stricter while - the () was optional before
Anders Halager <halager@gmail.com>
parents: 46
diff changeset
203 single statement | compound statement
b0a691727a0c Stricter while - the () was optional before
Anders Halager <halager@gmail.com>
parents: 46
diff changeset
204 */
11
642c6a998fd9 Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents: 10
diff changeset
205 case Tok.While:
46
90fb4fdfefdd While is back
Anders Halager <halager@gmail.com>
parents: 45
diff changeset
206 Token _while = lexer.next;
47
b0a691727a0c Stricter while - the () was optional before
Anders Halager <halager@gmail.com>
parents: 46
diff changeset
207 require(Tok.OpenParentheses);
46
90fb4fdfefdd While is back
Anders Halager <halager@gmail.com>
parents: 45
diff changeset
208 Exp cond = parseExpression();
47
b0a691727a0c Stricter while - the () was optional before
Anders Halager <halager@gmail.com>
parents: 46
diff changeset
209 require(Tok.CloseParentheses);
46
90fb4fdfefdd While is back
Anders Halager <halager@gmail.com>
parents: 45
diff changeset
210 Stmt bodyStmt = parseSingleOrCompoundStatement();
90fb4fdfefdd While is back
Anders Halager <halager@gmail.com>
parents: 45
diff changeset
211 return action.actOnWhileStmt(_while, cond, bodyStmt);
5
2c5a8f4c254a Added very simple if support.
Anders Halager <halager@gmail.com>
parents: 1
diff changeset
212
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
213 /*
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
214 One of four things:
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
215 A declaration of a function/variable `type id ...`
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
216 A direct assignment `id = exp;`
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
217 An indirect assignment `id.id = exp`
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
218 Some sort of free standing expression
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
219
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
220 The assignments should be handled as binary expressions?
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
221 */
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
222 case Tok.Identifier:
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
223 Token iden = lexer.peek;
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
224 Token n = lexer.peek(1);
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
225 // Must be an decl, if we start with a basic type, or two
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
226 // identifiers in a row
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
227 if (iden.isBasicType() || iden.isIdentifier())
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
228 {
81
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
229 if ( n.type == Tok.Star || n.type == Tok.OpenBracket)
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
230 {
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
231 int len = peekParseType;
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
232 if(lexer.peek(len).type == Tok.Identifier && len != 0)
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
233 return action.actOnDeclStmt(parseVarDecl());
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
234
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
235 Exp exp = parseExpression();
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
236 require(Tok.Seperator);
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
237 return action.actOnExprStmt(exp);
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
238 }
76
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
239
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
240 if ( n.isIdentifier())
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
241 return action.actOnDeclStmt(parseVarDecl());
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
242
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
243 // Expression: a.b, a = b, a(b) etc.
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
244 Exp exp = parseExpression();
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
245 require(Tok.Seperator);
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
246 return action.actOnExprStmt(exp);
76
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
247 break;
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
248 }
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
249
36
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
250 case Tok.Switch:
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
251 messages.report(UnexpectedTok, lexer.peek.location).arg(lexer.next.getType);
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
252 return null;
36
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
253
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
254 default:
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
255 if (t.isBasicType())
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
256 goto case Tok.Identifier;
80
682e20aa224f Pointers working now - big YAY
Anders Johnsen <skabet@gmail.com>
parents: 78
diff changeset
257 if (t.type == Tok.Star)
682e20aa224f Pointers working now - big YAY
Anders Johnsen <skabet@gmail.com>
parents: 78
diff changeset
258 {
682e20aa224f Pointers working now - big YAY
Anders Johnsen <skabet@gmail.com>
parents: 78
diff changeset
259 auto exp = parseExpression();
682e20aa224f Pointers working now - big YAY
Anders Johnsen <skabet@gmail.com>
parents: 78
diff changeset
260 require(Tok.Seperator);
682e20aa224f Pointers working now - big YAY
Anders Johnsen <skabet@gmail.com>
parents: 78
diff changeset
261 return action.actOnExprStmt(exp);
682e20aa224f Pointers working now - big YAY
Anders Johnsen <skabet@gmail.com>
parents: 78
diff changeset
262 }
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
263 messages.report(UnexpectedBeginStmt, lexer.peek.location).arg(lexer.next.getType);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
264 }
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
265 messages.report(UnexpectedTok, t.location);
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
266 return null;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
267 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
268
76
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
269 Decl parseVarDecl()
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
270 {
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
271 // manually hardcoded to only support "type id [= exp];"
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
272 // as that is the only thing the codegen understands
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
273 Id type = parseType;
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
274 Id id = Id(lexer.next);
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
275 Exp init;
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
276 if (skip(Tok.Assign))
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
277 init = parseExpression();
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
278 require(Tok.Seperator);
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
279 Decl d = action.actOnDeclarator(type, id, init);
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
280 return d;
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
281 }
9171f04dd9ee Now parsing varDecls a lot nicer!
Anders Johnsen <skabet@gmail.com>
parents: 75
diff changeset
282
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
283 /**
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
284 Parses a function/method given the already parsed return type and name
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
285 */
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
286 Decl parseFunc(ref Id type, ref Id name)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
287 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
288 Decl func = action.actOnStartOfFunctionDef(type, name);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
289 parseFuncArgs(func);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
290
82
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
291 if(lexer.peek.type == Tok.Seperator)
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
292 {
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
293 lexer.next;
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
294 return func;
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
295 }
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
296 Stmt stmt = parseCompoundStatement();
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
297
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
298 return action.actOnEndOfFunction(func, stmt);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
299 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
300
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
301 /**
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
302 Parse the function arguments, assumes current token is (.
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
303
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
304 Both the intitial paren and the ending paren is consumed.
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
305 */
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
306 void parseFuncArgs(Decl func)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
307 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
308 require(Tok.OpenParentheses); // Remove the "(" token.
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
309
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
310 while(lexer.peek.type != Tok.CloseParentheses)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
311 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
312 auto t = parseType();
82
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
313 Id i;
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
314 if(lexer.peek.type == Tok.Identifier)
06dda301ea61 Can declare outside functions and call c-functions
Anders Johnsen <skabet@gmail.com>
parents: 81
diff changeset
315 i = parseIdentifier();
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
316 action.addFuncArg(func, t, i);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
317
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
318 if(lexer.peek.type == Tok.Comma)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
319 lexer.next;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
320 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
321
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
322 require(Tok.CloseParentheses); // Remove the ")"
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
323 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
324
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
325 /**
45
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
326 Parse either a block, or a single statement as allowed after if, while
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
327 and for.
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
328 */
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
329 Stmt parseSingleOrCompoundStatement()
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
330 {
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
331 if (lexer.peek.type == Tok.OpenBrace)
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
332 return parseCompoundStatement();
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
333 return parseStatement();
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
334 }
9bc660cbdbec If statements are back
Anders Halager <halager@gmail.com>
parents: 44
diff changeset
335 /**
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
336 Parses a function-body or similar, expects { to be current token.
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
337
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
338 Will consume both the starting { and ending }
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
339 */
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
340 Stmt parseCompoundStatement()
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
341 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
342 Token lbrace = require(Tok.OpenBrace);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
343 SmallArray!(Stmt, 32) stmts; // Try to use the stack only
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
344 while (lexer.peek.type != Tok.CloseBrace)
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
345 stmts ~= parseStatement();
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
346 Token rbrace = require(Tok.CloseBrace);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
347 return action.actOnCompoundStmt(lbrace, rbrace, stmts.unsafe());
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
348 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
349
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
350 Id parseIdentifier()
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
351 {
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
352 Token tok = lexer.next;
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
353
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
354 if (tok.type is Tok.Identifier)
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
355 return Id(tok);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
356
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
357 messages.report(UnexpectedTokSingle, tok.location)
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
358 .arg(tok.getType)
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
359 .arg(Tok.Identifier);
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
360 }
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
361
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
362 /**
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
363 Parse a type - this includes pointer and array(at some point) types.
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
364 */
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
365 Id parseType()
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
366 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
367 Token type = lexer.next;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
368
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
369 Id currentType;
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
370
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
371 if ( !(type.isBasicType || type.type == Tok.Identifier) )
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: 89
diff changeset
372 messages.report(InvalidType, type.location);
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
373
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
374 currentType = Id(type);
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
375 type = lexer.peek;
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
376
81
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
377 while(type.type == Tok.Star || type.type == Tok.OpenBracket)
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
378 {
81
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
379 if(type.type == Tok.Star)
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
380 {
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
381 currentType = PointerId(currentType);
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
382 lexer.next;
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
383 }
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
384 else
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
385 {
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
386 lexer.next;
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: 89
diff changeset
387 if(lexer.peek.type == Tok.Integer)
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: 89
diff changeset
388 currentType = ArrayId(currentType, action.actOnNumericConstant(require(Tok.Integer)));
81
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
389 require(Tok.CloseBracket);
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
390
110c7e1c4ca2 Now you can declare array
Anders Johnsen <skabet@gmail.com>
parents: 80
diff changeset
391 }
75
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
392 type = lexer.peek;
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
393 }
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
394
86aec2160221 Parsing "int* x"
Anders Johnsen <skabet@gmail.com>
parents: 74
diff changeset
395 return currentType;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
396 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
397
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
398 int peekParseType()
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
399 {
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
400 int i;
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
401 Token type = lexer.peek(i);
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
402
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
403 Id currentType;
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
404
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
405 if ( !(type.isBasicType || type.type == Tok.Identifier) )
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
406 return 0;
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
407
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
408 currentType = Id(type);
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
409 type = lexer.peek(++i);
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
410
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
411 while(type.type == Tok.Star || type.type == Tok.OpenBracket)
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
412 {
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
413 if(type.type == Tok.Star)
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
414 {
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
415 i++;
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
416 }
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
417 else
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
418 {
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
419 if(lexer.peek(i++).type != Tok.OpenBracket)
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
420 return 0;
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: 89
diff changeset
421 if(lexer.peek(i).type == Tok.Integer)
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: 89
diff changeset
422 {
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: 89
diff changeset
423 i++;
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: 89
diff changeset
424 if(lexer.peek(i++).type != Tok.CloseBracket)
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: 89
diff changeset
425 return 0;
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: 89
diff changeset
426 }
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: 89
diff changeset
427 else
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: 89
diff changeset
428 if(lexer.peek(i++).type != Tok.CloseBracket)
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: 89
diff changeset
429 return 0;
84
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
430
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
431 }
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
432 type = lexer.peek(i);
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
433 }
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
434
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
435 return i;
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
436 }
cc05c041e6a3 Assign stuff to arrays.
Anders Johnsen <skabet@gmail.com>
parents: 83
diff changeset
437
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
438 private:
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
439 // -- Expression parsing -- //
83
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
440 Exp parsePostfixExp(Exp target)
28
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
441 {
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
442 switch(lexer.peek.type)
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
443 {
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
444 case Tok.Dot:
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
445 switch(lexer.peek(1).type)
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
446 {
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
447 case Tok.Identifier:
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
448 Token op = lexer.next;
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
449 Id member = Id(lexer.next);
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
450 Exp exp = action.actOnMemberReference(target, op.location, member);
83
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
451 return parsePostfixExp(exp);
28
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
452 default:
30
3147a52d1247 Ooops.. should have compiled before commit.. now works again
Anders Halager <halager@gmail.com>
parents: 29
diff changeset
453 Token t = lexer.peek(1);
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
454 messages.report(ExpectedIdAfterDot, t.location);
28
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
455 }
83
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
456 case Tok.OpenBracket:
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
457 Token open = lexer.next;
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
458 Exp index = parseExpression();
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
459 Token close = require(Tok.CloseBracket);
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
460 return action.actOnIndexEpr(target, open, index, close);
28
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
461 default:
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
462 return target;
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
463 }
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
464 }
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
465
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
466 Exp parseExpression(int p = 0)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
467 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
468 auto exp = P();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
469 Token next = lexer.peek();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
470 BinOp* op = null;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
471 while ((op = binary(next.type)) != null && op.prec >= p)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
472 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
473 lexer.next();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
474 int q = op.leftAssoc? 1 + op.prec : op.prec;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
475 auto exp2 = parseExpression(q);
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
476 exp = action.actOnBinaryOp(next.location, op.operator, exp, exp2);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
477 next = lexer.peek();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
478 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
479
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
480 return exp;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
481 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
482
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
483 Exp P()
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
484 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
485 Token next = lexer.next();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
486 if (auto op = unary(next.type))
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
487 return action.actOnUnaryOp(next, parseExpression(op.prec));
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
488 else if (next.type == Tok.OpenParentheses)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
489 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
490 auto e = parseExpression(0);
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
491 require(Tok.CloseParentheses);
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
492 return e;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
493 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
494 else if (next.type == Tok.Identifier)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
495 {
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
496 Exp value = action.actOnIdentifierExp(Id(next));
83
9e90694f5da0 Parse array indexing, and allow reading from arrays
Anders Halager <halager@gmail.com>
parents: 82
diff changeset
497 Exp iden = parsePostfixExp(value);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
498 switch(lexer.peek.type)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
499 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
500 case Tok.OpenParentheses:
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
501 Token lp = lexer.next;
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
502 SmallArray!(Exp, 8) args;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
503 while(lexer.peek.type != Tok.CloseParentheses)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
504 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
505 if(lexer.peek.type == Tok.Comma)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
506 lexer.next;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
507 args ~= parseExpression();
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
508 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
509
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
510 Token rp = lexer.next();
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
511 return action.actOnCallExpr(iden, lp, args.unsafe(), rp);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
512
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
513 default:
28
69464d465284 Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents: 22
diff changeset
514 return iden;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
515 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
516 }
68
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
517 else if (next.type == Tok.Cast)
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
518 return parseCast(next);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
519 else if (next.type == Tok.Integer)
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
520 return action.actOnNumericConstant(next);
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
521
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: 89
diff changeset
522 messages.report(ExpectedExp, next.location)
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: 89
diff changeset
523 .fatal(ExitLevel.Parser);
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
524 return null;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
525 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
526
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
527 Exp parseCast(ref Token _cast)
68
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
528 {
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
529 require(Tok.OpenParentheses);
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
530 auto next = lexer.next;
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
531 if(!next.isBasicType && !next.isIdentifier)
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
532 messages.report(ExpectedCastType, next.location);
68
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
533
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
534 require(Tok.CloseParentheses);
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
535 auto exp = P();
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
536 return action.actOnCastExpr(_cast, Id(next), exp);
68
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
537 }
381975d76baf A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents: 62
diff changeset
538
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
539 struct UnOp
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
540 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
541 Tok tokenType;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
542 int prec;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
543 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
544
78
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
545 static const UnOp[] _unary =
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
546 [
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
547 {Tok.Minus, 4},
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
548 {Tok.Star, 4}
ad956143dcdc Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents: 76
diff changeset
549 ];
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
550 UnOp* unary(Tok t)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
551 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
552 foreach (ref op; _unary)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
553 if (op.tokenType == t)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
554 return &op;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
555 return null;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
556 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
557
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
558 struct BinOp
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
559 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
560 Tok tokenType;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
561 int prec;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
562 bool leftAssoc;
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
563 Operator operator;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
564 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
565
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
566 static const BinOp[] _binary =
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
567 [
48
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
568 {Tok.Assign, 1, false, Operator.Assign},
b6c1dc30ca4b Only tests that dont pass now are structs and switches
Anders Halager <halager@gmail.com>
parents: 47
diff changeset
569
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
570 {Tok.Eq, 2, true, Operator.Eq},
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
571 {Tok.Ne, 2, true, Operator.Ne},
10
2f493057cf17 Some support for the rest of the boolean operators
Anders Halager <halager@gmail.com>
parents: 7
diff changeset
572
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
573 {Tok.Lt, 2, true, Operator.Lt},
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
574 {Tok.Le, 2, true, Operator.Le},
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
575 {Tok.Gt, 2, true, Operator.Gt},
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
576 {Tok.Ge, 2, true, Operator.Ge},
10
2f493057cf17 Some support for the rest of the boolean operators
Anders Halager <halager@gmail.com>
parents: 7
diff changeset
577
74
192da4976daa Renamed Add, Sub, Mul, Div and Mod in lexer to what they are (Plus, Minus....)
johnsen@johnsen-laptop
parents: 71
diff changeset
578 {Tok.Plus, 3, true, Operator.Add},
192da4976daa Renamed Add, Sub, Mul, Div and Mod in lexer to what they are (Plus, Minus....)
johnsen@johnsen-laptop
parents: 71
diff changeset
579 {Tok.Minus, 3, true, Operator.Sub},
44
495188f9078e Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents: 37
diff changeset
580
74
192da4976daa Renamed Add, Sub, Mul, Div and Mod in lexer to what they are (Plus, Minus....)
johnsen@johnsen-laptop
parents: 71
diff changeset
581 {Tok.Star, 5, true, Operator.Mul},
192da4976daa Renamed Add, Sub, Mul, Div and Mod in lexer to what they are (Plus, Minus....)
johnsen@johnsen-laptop
parents: 71
diff changeset
582 {Tok.Slash, 5, true, Operator.Div},
192da4976daa Renamed Add, Sub, Mul, Div and Mod in lexer to what they are (Plus, Minus....)
johnsen@johnsen-laptop
parents: 71
diff changeset
583 {Tok.Percent, 5, true, Operator.Mod}
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
584 ];
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
585 BinOp* binary(Tok t)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
586 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
587 foreach (ref op; _binary)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
588 if (op.tokenType == t)
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
589 return &op;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
590 return null;
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
591 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
592
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
593 private:
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
594
36
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
595 Token require(Tok t)
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
596 {
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
597 if (lexer.peek().type != t)
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
598 messages.report(UnexpectedTokSingle, lexer.peek.location)
29
41d23f2762c3 Merge, and updated Error class
Anders Halager <halager@gmail.com>
parents: 28
diff changeset
599 .arg(lexer.peek.getType)
89
a49bb982a7b0 Using the new SourceLocation system to handle errors. Also, this is the first push for making the errors don't throw, but continue to check the source.
Anders Johnsen <skabet@gmail.com>
parents: 88
diff changeset
600 .arg(t);
36
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
601 return lexer.next();
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
602 }
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
603
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
604 bool skip(Tok t)
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
605 {
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
606 if (lexer.peek().type != t)
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
607 return false;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
608 lexer.next();
36
ce17bea8e9bd Switch statements support
Anders Halager <halager@gmail.com>
parents: 33
diff changeset
609 return true;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
610 }
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
611
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
612 Lexer lexer;
88
eb5b2c719a39 Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents: 84
diff changeset
613 SourceManager sm;
1
2168f4cb73f1 First push
johnsen@johnsen-desktop
parents:
diff changeset
614 }