diff options
| author | Cliff Click <cliffc@acm.org> | 2026-10-03 14:03:48 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-10-03 14:03:48 -0700 |
| commit | 22df891893d30e9906d0b14fc3ae5d8e67d500b2 (patch) | |
| tree | 2bda142f92febc8b26f1cabd4bb0c46d1864f34b /chapter16/src/main/java | |
| parent | c3958e34c0de21df52e57e47e4f47ffe6464f138 (diff) | |
| download | Simple-22df891893d30e9906d0b14fc3ae5d8e67d500b2.tar.gz Simple-22df891893d30e9906d0b14fc3ae5d8e67d500b2.zip | |
Chapter renumber, and split ch 17
Renumber chapters 10a, 10b, 11, 12, 13, 14 -> 10, 11, 13, 12, [11,14].
Split ch 17 into a 17a and 17b; shuffle around im/mutable syntax to be consistent, and present it more slowly.
THe new immutable syntax is pushed through all later chapters
Diffstat (limited to 'chapter16/src/main/java')
| -rw-r--r-- | chapter16/src/main/java/com/seaofnodes/simple/Parser.java | 26 |
1 files changed, 7 insertions, 19 deletions
diff --git a/chapter16/src/main/java/com/seaofnodes/simple/Parser.java b/chapter16/src/main/java/com/seaofnodes/simple/Parser.java index a7d31584..97b29a37 100644 --- a/chapter16/src/main/java/com/seaofnodes/simple/Parser.java +++ b/chapter16/src/main/java/com/seaofnodes/simple/Parser.java @@ -402,7 +402,7 @@ public class Parser { /** Parse: name [=expr] */ - private Node parseAsgn(Type t, boolean xfinal) { + private Node parseAsgn(Type t) { boolean isDecl = t!=null; // Having a type is a declaration, missing one is updating a prior name int old = _lexer._position; String name = requireId(); @@ -411,7 +411,7 @@ public class Parser { // Bare "name" is not allowed if( t==null ) throw errorSyntax("expression"); // Does this declaration need an initializer and not getting one? - Type init = xfinal || t instanceof TypeMemPtr tmp && !tmp._nil + Type init = t instanceof TypeMemPtr tmp && !tmp._nil // Must be initialized in the "new" constructor ? Type.TOP // Else takes the default @@ -430,11 +430,6 @@ public class Parser { ScopeNode.Var def = _scope.lookup(name); if( def==null ) throw error("Undefined name '" + name + "'"); - // TOP fields are for late-initialized fields; these have never - // been written to, and this must be the final write. Other writes - // need to check the final bit. - if( _scope.in(def._idx)._type!=Type.TOP && def._final ) - throw error("Cannot reassign final '"+name+"'"); t = def._type; // Declared field type } @@ -452,20 +447,13 @@ public class Parser { throw error("Type " + e.str() + " is not of declared type " + t.str()); if( isDecl ) { - if( !_scope.define(name,t,xfinal,expr) ) + if( !_scope.define(name,t,false,expr) ) throw error("Redefining name '" + name + "'"); } else _scope.update(name,expr); return expr; } - /** Parse final: [!]asgn - */ - private Node parseFinal(Type t) { - boolean xfinal = t!=null && match("!"); - return parseAsgn(t,xfinal); - } - /** * Parses an expression statement or a declaration statement where type is a struct * @@ -482,13 +470,13 @@ public class Parser { Node n; Type t = type(); if( t != null ) { - // now parse final [, final]* - n = parseFinal(t); + // Parse comma-separated declarations + n = parseAsgn(t); while( match(",") ) - n = parseFinal(t); + n = parseAsgn(t); // Parse "asgn;" which is just "name = expr;" - } else if( ( n = parseAsgn(null,false)) == null ) { + } else if( ( n = parseAsgn(null)) == null ) { // Something else n = parseExpression(); } |
