Procházet zdrojové kódy

js -> eberon transition

Vladislav Folts před 10 roky
rodič
revize
2e955b4dec
8 změnil soubory, kde provedl 124 přidání a 72 odebrání
  1. binární
      bin/compiled.zip
  2. 0 54
      src/context.js
  3. 4 3
      src/grammar.js
  4. 10 0
      src/ob/Chars.ob
  5. 89 1
      src/ob/ContextHierarchy.ob
  6. 6 6
      src/ob/Lexer.ob
  7. 9 1
      src/ob/String.ob
  8. 6 7
      src/ob/Types.ob

binární
bin/compiled.zip


+ 0 - 54
src/context.js

@@ -28,54 +28,6 @@ var ChainedContext = ContextHierarchy.Node;
 ChainedContext.extend = Class.extend;
 ChainedContext.prototype.init = ContextHierarchy.Node;
 
-exports.Integer = ChainedContext.extend({
-    init: function IntegerContext(context){
-        ChainedContext.prototype.init.call(this, context);
-    },
-    endParse: function(){
-        var n = this.attributes.int;
-        this.parent().handleConst(basicTypes.integer, Code.makeIntConst(n), n.toString());
-    }
-});
-
-exports.Real = ChainedContext.extend({
-    init: function RealContext(context){
-        ChainedContext.prototype.init.call(this, context);
-    },
-    endParse: function(){
-        var n = this.attributes.real;
-        this.parent().handleConst(basicTypes.real, Code.makeRealConst(n), n.toString());
-    }
-});
-
-function escapeString(s){
-    var escapeChars = {"\\": "\\\\",
-                       "\"": "\\\"",
-                       "\n": "\\n",
-                       "\r": "\\r",
-                       "\t": "\\t",
-                       "\b": "\\b",
-                       "\f": "\\f"
-                      };
-    var result = "\"";
-    for(var i = 0; i < s.length; ++i){
-        var c = s[i];
-        var escape = escapeChars[c];
-        result += escape !== undefined ? escape : c;
-    }
-    return result + "\"";
-}
-
-exports.String = ChainedContext.extend({
-    init: function StringContext(context){
-        ChainedContext.prototype.init.call(this, context);
-    },
-    endParse: function(){
-        var s = this.attributes.str;
-        this.parent().handleConst(new Type.String(s), Code.makeStringConst(s), escapeString(s));
-    }
-});
-
 exports.BaseType = ChainedContext.extend({
     init: function BaseTypeContext(context){
         ChainedContext.prototype.init.call(this, context);
@@ -1072,12 +1024,6 @@ exports.SetElement = ChainedContext.extend({
     }
 });
 
-function constValueCode(value){
-    if (typeof value == "string")
-        return escapeString(value);
-    return value.toString();
-}
-
 exports.SimpleExpression = ChainedContext.extend({
     init: function SimpleExpressionContext(context){
         ChainedContext.prototype.init.call(this, context);

+ 4 - 3
src/grammar.js

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

+ 10 - 0
src/ob/Chars.ob

@@ -0,0 +1,10 @@
+MODULE Chars;
+CONST
+    doubleQuote* = 22X;
+    backspace* = 08X;
+    tab* = 09X;
+    ln* = 0AX;
+    feed* = 0CX;
+    cr* = 0DX;
+    backslash* = "\";
+END Chars.

+ 89 - 1
src/ob/ContextHierarchy.ob

@@ -1,5 +1,5 @@
 MODULE ContextHierarchy;
-IMPORT Cast, Code, CodeGenerator, Errors, LanguageContext, Module, Scope, Symbols, String, Types;
+IMPORT Cast, Chars, Code, CodeGenerator, Errors, LanguageContext, Module, Scope, Symbols, String, Types;
 TYPE
     PRoot = POINTER TO Root;
     PNode = POINTER TO Node;
@@ -54,6 +54,28 @@ TYPE
         gen: INTEGER;
     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)
     | mParent(parent);
 BEGIN
@@ -153,6 +175,72 @@ PROCEDURE Root.root(): PRoot;
     RETURN SELF(POINTER);
 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;
 BEGIN
     s <- cx.findSymbol(id);

+ 6 - 6
src/ob/Lexer.ob

@@ -1,8 +1,8 @@
 MODULE Lexer;
-IMPORT Context, ContextHierarchy, Errors, Stream, String;
+IMPORT Chars, Context, ContextHierarchy, Errors, Stream, String;
 
 CONST
-    quote = 22X; (* " *)
+    doubleQuote = Chars.doubleQuote;
     commentBegin = "(*";
     commentEnd = "*)";
 
@@ -207,8 +207,8 @@ PROCEDURE string*(VAR stream: Stream.Type; VAR context: ContextHierarchy.Node):
     BEGIN
         IF ~Stream.eof(stream) THEN
             c := Stream.getChar(stream);
-            WHILE (c # quote) & ~Stream.eof(stream) DO
-                IF c # quote THEN
+            WHILE (c # doubleQuote) & ~Stream.eof(stream) DO
+                IF c # doubleQuote THEN
                     s := s + String.fromChar(c);
                 END;
                 c := Stream.getChar(stream);
@@ -217,7 +217,7 @@ PROCEDURE string*(VAR stream: Stream.Type; VAR context: ContextHierarchy.Node):
             c := 0X;
         END;
         
-        IF c # quote THEN
+        IF c # doubleQuote THEN
             Errors.raise("unexpected end of string");
         END;
         
@@ -242,7 +242,7 @@ BEGIN
     result <- FALSE;
     IF ~Stream.eof(stream) THEN
         c <- Stream.getChar(stream);
-        IF c = quote THEN
+        IF c = doubleQuote THEN
             quotedString();
             result := TRUE;
         ELSIF isDigit(c) THEN

+ 9 - 1
src/ob/String.ob

@@ -15,7 +15,15 @@ VAR
 BEGIN
     JS.do("result = '' + i");
     RETURN result
-END fromInt;
+END;
+
+PROCEDURE fromReal*(r: REAL): STRING;
+VAR 
+    result: STRING;
+BEGIN
+    JS.do("result = '' + r");
+    RETURN result
+END;
 
 PROCEDURE parseReal*(s: STRING): REAL;
 VAR 

+ 6 - 7
src/ob/Types.ob

@@ -1,6 +1,6 @@
 MODULE Types;
 IMPORT
-    Context, Errors, JS, OberonRtl, Object, ScopeBase, Str := String;
+    Chars, Context, Errors, JS, OberonRtl, Object, ScopeBase, Str := String;
 TYPE
     Id* = RECORD(Object.Type)
         PROCEDURE idType*(): STRING
@@ -487,14 +487,13 @@ PROCEDURE FieldVariable.type(): PStorageType;
 END;
 
 PROCEDURE FieldVariable.referenceCode(): STRING;
-CONST
-    kQuote = 22X;
 VAR
     result: STRING;
 BEGIN
     codeId <- mangleField(SELF.field.mIdentdef.id());
     IF SELF.type().isScalar() THEN
-        result := SELF.rtl.makeRef(SELF.leadCode, kQuote + codeId + kQuote);
+        result := SELF.rtl.makeRef(SELF.leadCode, 
+                                   Chars.doubleQuote + codeId + Chars.doubleQuote);
     ELSE
         result := SELF.leadCode + "." + codeId;
     END;
@@ -944,11 +943,11 @@ PROCEDURE RecordField.identdef(): Context.PIdentdefInfo;
 END;
 
 PROCEDURE RecordField.designatorCode(leadCode: STRING; cx: Context.Type): PFieldCode;
-CONST
-    kQuote = 22X;
 BEGIN
     codeId <- mangleField(SELF.mIdentdef.id());
-    RETURN NEW FieldCode(leadCode + "." + codeId, leadCode, kQuote + codeId + kQuote);
+    RETURN NEW FieldCode(leadCode + "." + codeId, 
+                         leadCode, 
+                         Chars.doubleQuote + codeId + Chars.doubleQuote);
 END;
 
 PROCEDURE RecordField.type(): PType;