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

Method alloc

chapter23/src/main/java/com/seaofnodes/simple/Parser.java:1321–1371  ·  view source on GitHub ↗

Parse an allocation

()

Source from the content-addressed store, hash-verified

1319
1320 // Load local value
1321 Node rvalue = _scope.in(var);
1322 if( rvalue._type == Type.BOTTOM )
1323 if( rvalue instanceof FRefNode )
1324 return parsePostfix(rvalue);
1325 else throw error("Cannot read uninitialized field '"+id+"'");
1326
1327 // Check for assign-update, x += e0;
1328 char ch = pre==0 ? _lexer.matchOperAssign() : (char)pre;
1329 if( ch==0 ) // Normal primary, check for postfix updates
1330 return parsePostfix(rvalue);
1331 // Assign-update direct into Scope
1332 if( var._final )
1333 throw error("Cannot reassign final '"+var._name+"'");
1334 Node op = opAssign(ch,rvalue, var.type() );
1335 _scope.update(var,op);
1336 if( postfix(ch) ) rvalue.unkeep();
1337 return pre==0 && postfix(ch) ? rvalue : op;
1338 }
1339
1340 // true if instance variable lookup (var is in any number of nested blocks,
1341 // then an instance function then inside a struct declaration).
1342 private boolean instance(Var var ) {
1343 // If a method var, allow nested blocks and methods, but not fcns or other structs
1344 Kind kind = _scope.kind(var);
1345 if( kind instanceof Kind.Define ) {
1346 for( int i = _scope.depth()-1; i>=0 && var._idx < _scope._kinds.at(i)._lexSize; i-- ) {
1347 Kind xkind = _scope._kinds.at(i);
1348 if( xkind instanceof Kind.Block ) continue;
1349 if( xkind instanceof Kind.Func ) {
1350 if( _scope._kinds.at(i-1)==kind )
1351 return true; // Instance variable: lookup in a method in a decl
1352 }
1353 throw Utils.TODO();
1354 }
1355 } else {
1356 // If a normal var, allow any nested blocks, and a last fcn -
1357 // but not functions further out (which involves capture).
1358 Node def = _scope.in(var._idx);
1359 for( int i = _scope.depth()-1; i>=0 && var._idx < _scope._kinds.at(i)._lexSize; i-- )
1360 if( _scope._kinds.at(i) instanceof Kind.Func &&
1361 !(def instanceof FRefNode) && !(var._final && def._type.isConstant()) )
1362 throw error("Variable '"+var._name+"' is out of function scope and must be a final constant");
1363 }
1364 return false;
1365 }
1366
1367 // Check for assign-update, "x += e0".
1368 // Returns "x + e0"; the caller assigns value.
1369 // if ch is postfix, then lhs is kept and caller will unkeep and return it.
1370 private Node opAssign( char ch, Node lhs, Type t ) {
1371 // RHS of the update.
1372 lhs.keep(); // Alive across parseAsgn
1373 Node rhs =
1374 (byte)ch == 1 ? con( 1) : // var++

Callers 1

parsePrimaryMethod · 0.95

Calls 15

typeMethod · 0.95
errorMethod · 0.95
matchMethod · 0.95
makeZeroMethod · 0.95
parseAsgnMethod · 0.95
requireMethod · 0.95
typeAryMethod · 0.95
newArrayMethod · 0.95
strMethod · 0.95
locMethod · 0.95
conMethod · 0.95
parseBlockMethod · 0.95

Tested by

no test coverage detected