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

Method parse

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

Source from the content-addressed store, hash-verified

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

Callers 4

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

Calls 15

ctrlMethod · 0.95
parseStatementMethod · 0.95
parseFunctionBodyMethod · 0.95
locMethod · 0.95
retMethod · 0.95
uctrlMethod · 0.95
errorMethod · 0.95
_errorSyntaxMethod · 0.95
defineMethod · 0.45
memMethod · 0.45
isEOFMethod · 0.45
skipWhiteSpaceMethod · 0.45

Tested by

no test coverage detected