MCPcopy Create free account
hub / github.com/SeaOfNodes/Simple / parse

Method parse

chapter24/src/main/java/com/seaofnodes/simple/Parser.java:135–195  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

133
134 public void parse() {
135
136 _scope.define(ScopeNode.CTRL, Type.CONTROL , false, null, _lexer);
137 _scope.define(ScopeNode.MEM0, TypeMem.BOT , false, null, _lexer);
138 _scope.define(ScopeNode.ARG0, TypeInteger.BOT, false, null, _lexer);
139
140 ctrl(XCTRL);
141 _scope.mem(new MemMergeNode(false));
142
143 // Parse the sys import
144 _lexer = new Lexer(sys.SYS);
145 while( !_lexer.isEOF() ) {
146 parseStatement();
147 _lexer.skipWhiteSpace();
148 }
149
150 // Reset lexer for program text
151 _lexer = new Lexer(_code._src);
152 _xScopes.push(_scope);
153
154 // Parse whole program, as-if function header "{ int arg -> body }"
155 parseFunctionBody( _code._main,loc(),"arg");
156
157 // Kill an empty default main. Keep only if it was explicitly defined
158 // (programmer asked for a "main") or it has stuff (i.e. beginner
159 // default main).
160 FunNode main = _code.link(_code._main);
161 StopNode stop = _code._stop;
162 // Gather an explicit main
163 FunNode xmain = null;
164 for( Node n : stop._inputs )
165 if( n instanceof ReturnNode ret && "main".equals(ret.fun()._name) )
166 if( xmain != null ) throw Utils.TODO();
167 else xmain = ret.fun();
168
169 if( main.ret().expr()._type==Type.TOP && main.uctrl()==null ) {
170 // Kill an empty default main; so it does not attempt to put a
171 // "main" in any final ELF file
172 main.setDef(1,XCTRL); // Delete default start input
173 stop.delDef(stop._inputs.find(main.ret()));
174 if( xmain != null )
175 _code.setMain(xmain);
176
177 } else {
178 // We have a non-empty default main.
179 if( xmain != null )
180 // Found an explicit "main" AND we have a default "main"
181 throw error("Cannot define both an explicit main and a default main");
182 main._name = "main";
183 }
184
185 if( !_lexer.isEOF() ) throw _errorSyntax("unexpected");
186
187 // Clean up and reset
188 _xScopes.pop();
189 _scope.kill();
190 for( StructNode init : INITS.values() )
191 init.unkeep().kill();
192 INITS.clear();

Callers 5

d3.v7.min.jsFile · 0.45
webtcp.min.jsFile · 0.45
fFunction · 0.45
WebTCPFunction · 0.45
parseChapterMethod · 0.45

Calls 15

ctrlMethod · 0.95
parseStatementMethod · 0.95
parseFunctionBodyMethod · 0.95
locMethod · 0.95
TODOMethod · 0.95
retMethod · 0.95
uctrlMethod · 0.95
errorMethod · 0.95
_errorSyntaxMethod · 0.95
setMainMethod · 0.80
defineMethod · 0.45
memMethod · 0.45

Tested by 1

parseChapterMethod · 0.36