Browse Source

factor out expression contexts

Vladislav Folts 10 years ago
parent
commit
729b2ac7c4
5 changed files with 106 additions and 106 deletions
  1. BIN
      bin/compiled.zip
  2. 4 4
      src/grammar.js
  3. 91 0
      src/ob/ContextExpression.ob
  4. 2 93
      src/ob/ContextHierarchy.ob
  5. 9 9
      src/ob/Lexer.ob

BIN
bin/compiled.zip


+ 4 - 4
src/grammar.js

@@ -1,7 +1,7 @@
 "use strict";
 "use strict";
 
 
 var Context = require("context.js");
 var Context = require("context.js");
-var ContextHierarchy = require("js/ContextHierarchy.js");
+var ContextExpression = require("js/ContextExpression.js");
 var Lexer = require("js/Lexer.js");
 var Lexer = require("js/Lexer.js");
 var Parser = require("parser.js");
 var Parser = require("parser.js");
 var Class = require("rtl.js").Class;
 var Class = require("rtl.js").Class;
@@ -65,10 +65,10 @@ var type = or(context(qualident, Context.Type),
 var identList = and(identdef, repeat(and(",", identdef)));
 var identList = and(identdef, repeat(and(",", identdef)));
 var variableDeclaration = context(and(identList, ":", type), contexts.variableDeclaration);
 var variableDeclaration = context(and(identList, ":", type), contexts.variableDeclaration);
 
 
-var integer = context(Lexer.integer, ContextHierarchy.Integer);
-var real = context(Lexer.real, ContextHierarchy.Real);
+var integer = context(Lexer.integer, ContextExpression.Integer);
+var real = context(Lexer.real, ContextExpression.Real);
 var number = or(real, integer);
 var number = or(real, integer);
-var string = context(Lexer.string, ContextHierarchy.Str);
+var string = context(Lexer.string, ContextExpression.Str);
 
 
 var factor = context(
 var factor = context(
     or(string, 
     or(string, 

+ 91 - 0
src/ob/ContextExpression.ob

@@ -0,0 +1,91 @@
+MODULE ContextExpression;
+IMPORT 
+    Chars, Code, ContextHierarchy, String, Types;
+TYPE
+    Factor* = RECORD(ContextHierarchy.Node)
+        PROCEDURE handleConst(type: Types.PType; value: Code.PConst; code: STRING);
+    END;
+    PFactor = POINTER TO Factor;
+
+    Const = RECORD(ContextHierarchy.Node)
+        PROCEDURE Const(factor: PFactor);
+
+        factor: PFactor;
+    END;
+
+    Integer* = RECORD(Const)
+        PROCEDURE handleInt*(n: INTEGER);
+    END;
+
+    Real* = RECORD(Const)
+        PROCEDURE handleReal*(r: REAL);
+    END;
+
+    Str* = RECORD(Const)
+        PROCEDURE handleStr*(s: STRING);
+    END;
+
+PROCEDURE Const.Const(factor: PFactor)
+    | SUPER(factor),
+      factor(factor);
+END;
+
+PROCEDURE Integer.handleInt(n: INTEGER);
+BEGIN
+    SELF.factor.handleConst(
+        Types.basic.integer, 
+        Code.makeIntConst(n), 
+        String.fromInt(n));
+END;
+
+PROCEDURE Real.handleReal(r: REAL);
+BEGIN
+    SELF.factor.handleConst(
+        Types.basic.real, 
+        Code.makeRealConst(r), 
+        String.fromReal(r));
+END;
+
+PROCEDURE escapeString(s: STRING): STRING;
+CONST
+    doubleQuote = Chars.doubleQuote;
+    ln          = Chars.ln;
+    cr          = Chars.cr;
+    tab         = Chars.tab;
+    backspace   = Chars.backspace;
+    feed        = Chars.feed;
+    backslash   = Chars.backslash;
+VAR
+    result: STRING;
+BEGIN
+    result := doubleQuote;
+    from <- 0;
+    FOR i <- 0 TO LEN(s) - 1 DO
+        escape <- CHR(0);
+        CASE s[i] OF
+              backslash:    escape := backslash;
+            | doubleQuote:  escape := doubleQuote;
+            | ln:           escape := "n";
+            | cr:           escape := "r";
+            | tab:          escape := "t";
+            | backspace:    escape := "b";
+            | feed:         escape := "f";
+        END;
+
+        IF ORD(escape) # 0 THEN
+            result := result + String.substr(s, from, i - from) + backslash + String.fromChar(escape);
+            from := i + 1; 
+        END;
+    END;
+    RETURN result + String.substr(s, from, LEN(s) - from) + doubleQuote;
+END;
+
+PROCEDURE Str.handleStr(s: STRING);
+BEGIN
+    SELF.factor.handleConst(
+        NEW Types.String(s), 
+        Code.makeStringConst(s), 
+        escapeString(s));
+END;
+
+END ContextExpression.

+ 2 - 93
src/ob/ContextHierarchy.ob

@@ -1,5 +1,5 @@
 MODULE ContextHierarchy;
 MODULE ContextHierarchy;
-IMPORT Cast, Chars, Code, CodeGenerator, Errors, LanguageContext, Module, Scope, Symbols, String, Types;
+IMPORT Cast, Code, CodeGenerator, Errors, LanguageContext, Module, Scope, Symbols, String, Types;
 TYPE
 TYPE
     PRoot = POINTER TO Root;
     PRoot = POINTER TO Root;
     PNode = POINTER TO Node;
     PNode = POINTER TO Node;
@@ -17,13 +17,10 @@ TYPE
     END;
     END;
 
 
     Attributes* = RECORD
     Attributes* = RECORD
-        int*: INTEGER;
-        real*: REAL;
-        str*: STRING;
     END;
     END;
 
 
     Node* = RECORD
     Node* = RECORD
-        PROCEDURE Node(parent: PNode);
+        PROCEDURE Node*(parent: PNode);
 
 
         PROCEDURE root(): PRoot;
         PROCEDURE root(): PRoot;
         PROCEDURE parent(): PNode;
         PROCEDURE parent(): PNode;
@@ -54,28 +51,6 @@ TYPE
         gen: INTEGER;
         gen: INTEGER;
     END;
     END;
 
 
-    Factor* = RECORD(Node)
-        PROCEDURE handleConst(type: Types.PType; value: Code.PConst; code: STRING);
-    END;
-    PFactor = POINTER TO Factor;
-
-    Const = RECORD(Node)
-        PROCEDURE Const(factor: PFactor);
-
-        PROCEDURE endParse();
-
-        factor: PFactor;
-    END;
-
-    Integer* = RECORD(Const)
-    END;
-
-    Real* = RECORD(Const)
-    END;
-
-    Str* = RECORD(Const)
-    END;
-
 PROCEDURE Node.Node(parent: PNode)
 PROCEDURE Node.Node(parent: PNode)
     | mParent(parent);
     | mParent(parent);
 BEGIN
 BEGIN
@@ -175,72 +150,6 @@ PROCEDURE Root.root(): PRoot;
     RETURN SELF(POINTER);
     RETURN SELF(POINTER);
 END;
 END;
 
 
-PROCEDURE Const.Const(factor: PFactor)
-    | SUPER(factor),
-      factor(factor);
-END;
-
-PROCEDURE Integer.endParse();
-BEGIN
-    n <- SELF.attributes.int;
-    SELF.factor.handleConst(
-        Types.basic.integer, 
-        Code.makeIntConst(n), 
-        String.fromInt(n));
-END;
-
-PROCEDURE Real.endParse();
-BEGIN
-    r <- SELF.attributes.real;
-    SELF.factor.handleConst(
-        Types.basic.real, 
-        Code.makeRealConst(r), 
-        String.fromReal(r));
-END;
-
-PROCEDURE escapeString(s: STRING): STRING;
-CONST
-    doubleQuote = Chars.doubleQuote;
-    ln          = Chars.ln;
-    cr          = Chars.cr;
-    tab         = Chars.tab;
-    backspace   = Chars.backspace;
-    feed        = Chars.feed;
-    backslash   = Chars.backslash;
-VAR
-    result: STRING;
-BEGIN
-    result := doubleQuote;
-    from <- 0;
-    FOR i <- 0 TO LEN(s) - 1 DO
-        escape <- CHR(0);
-        CASE s[i] OF
-              backslash:    escape := backslash;
-            | doubleQuote:  escape := doubleQuote;
-            | ln:           escape := "n";
-            | cr:           escape := "r";
-            | tab:          escape := "t";
-            | backspace:    escape := "b";
-            | feed:         escape := "f";
-        END;
-
-        IF ORD(escape) # 0 THEN
-            result := result + String.substr(s, from, i - from) + backslash + String.fromChar(escape);
-            from := i + 1; 
-        END;
-    END;
-    RETURN result + String.substr(s, from, LEN(s) - from) + doubleQuote;
-END;
-
-PROCEDURE Str.endParse();
-BEGIN
-    s <- SELF.attributes.str;
-    SELF.factor.handleConst(
-        NEW Types.String(s), 
-        Code.makeStringConst(s), 
-        escapeString(s));
-END;
-
 PROCEDURE getSymbolAndScope*(cx: Root; id: STRING): Symbols.PFoundSymbol;
 PROCEDURE getSymbolAndScope*(cx: Root; id: STRING): Symbols.PFoundSymbol;
 BEGIN
 BEGIN
     s <- cx.findSymbol(id);
     s <- cx.findSymbol(id);

+ 9 - 9
src/ob/Lexer.ob

@@ -1,5 +1,5 @@
 MODULE Lexer;
 MODULE Lexer;
-IMPORT Chars, Context, ContextHierarchy, Errors, Stream, String;
+IMPORT Chars, Context, ContextExpression, ContextHierarchy, Errors, Stream, String;
 
 
 CONST
 CONST
     doubleQuote = Chars.doubleQuote;
     doubleQuote = Chars.doubleQuote;
@@ -45,7 +45,7 @@ BEGIN
     RETURN result;
     RETURN result;
 END;
 END;
 
 
-PROCEDURE integer*(VAR stream: Stream.Type; VAR context: ContextHierarchy.Node): BOOLEAN;
+PROCEDURE integer*(VAR stream: Stream.Type; VAR cx: ContextExpression.Integer): BOOLEAN;
 VAR
 VAR
     hexDetected: BOOLEAN;
     hexDetected: BOOLEAN;
     dec, hex: INTEGER;
     dec, hex: INTEGER;
@@ -83,9 +83,9 @@ BEGIN
 
 
         IF peekSeparator(stream) THEN
         IF peekSeparator(stream) THEN
             IF hexDetected THEN
             IF hexDetected THEN
-                context.attributes.int := hex;
+                cx.handleInt(hex);
             ELSE
             ELSE
-                context.attributes.int := dec;
+                cx.handleInt(dec);
             END;
             END;
         
         
             result := TRUE;
             result := TRUE;
@@ -94,7 +94,7 @@ BEGIN
     RETURN result;
     RETURN result;
 END;
 END;
 
 
-PROCEDURE real*(VAR stream: Stream.Type; VAR context: ContextHierarchy.Node): BOOLEAN;
+PROCEDURE real*(VAR stream: Stream.Type; VAR cx: ContextExpression.Real): BOOLEAN;
 VAR 
 VAR 
     c: CHAR;
     c: CHAR;
     s: STRING;
     s: STRING;
@@ -174,7 +174,7 @@ BEGIN
         s := s + ".";
         s := s + ".";
         collectOptionalDigits();
         collectOptionalDigits();
         IF collectScale() & peekSeparator(stream) THEN
         IF collectScale() & peekSeparator(stream) THEN
-            context.attributes.real := String.parseReal(s);
+            cx.handleReal(String.parseReal(s));
             result := TRUE;
             result := TRUE;
         END
         END
     END;
     END;
@@ -198,7 +198,7 @@ BEGIN
     RETURN result;
     RETURN result;
 END;
 END;
 
 
-PROCEDURE string*(VAR stream: Stream.Type; VAR context: ContextHierarchy.Node): BOOLEAN;
+PROCEDURE string*(VAR stream: Stream.Type; VAR cx: ContextExpression.Str): BOOLEAN;
 
 
     PROCEDURE quotedString();
     PROCEDURE quotedString();
     VAR
     VAR
@@ -221,7 +221,7 @@ PROCEDURE string*(VAR stream: Stream.Type; VAR context: ContextHierarchy.Node):
             Errors.raise("unexpected end of string");
             Errors.raise("unexpected end of string");
         END;
         END;
         
         
-        context.attributes.str := s;
+        cx.handleStr(s);
     END;    
     END;    
 
 
     PROCEDURE hexString(firstChar: CHAR): BOOLEAN;
     PROCEDURE hexString(firstChar: CHAR): BOOLEAN;
@@ -232,7 +232,7 @@ PROCEDURE string*(VAR stream: Stream.Type; VAR context: ContextHierarchy.Node):
             s := s + String.fromChar(Stream.getChar(stream));
             s := s + String.fromChar(Stream.getChar(stream));
         END;
         END;
         IF ~Stream.eof(stream) & (Stream.getChar(stream) = "X") THEN
         IF ~Stream.eof(stream) & (Stream.getChar(stream) = "X") THEN
-            context.attributes.str := String.fromChar(CHR(String.parseHex(s)));
+            cx.handleStr(String.fromChar(CHR(String.parseHex(s))));
             result := TRUE;
             result := TRUE;
         END;
         END;
         RETURN result;
         RETURN result;