Browse Source

in place variables and string literals

Vladislav Folts 11 years ago
parent
commit
3facac9b81

BIN
bin/compiled.zip


+ 10 - 11
src/eberon/eberon_context.js

@@ -131,15 +131,6 @@ var TypeNarrowVariable = Type.Variable.extend({
         this.__invertedType = this.__type;
         this.__invertedType = this.__type;
         this.__isRef = isRef;
         this.__isRef = isRef;
         this.__isReadOnly = isReadOnly;
         this.__isReadOnly = isReadOnly;
-
-        /*
-        var d = e.designator();
-        if (d) {
-            var v = d.info();
-            if (v instanceof Type.Variable)
-                this.__isRef = v.isReference();
-        }
-        */
     },
     },
     type: function(){
     type: function(){
         return this.__type;
         return this.__type;
@@ -169,6 +160,13 @@ var TypeNarrowVariable = Type.Variable.extend({
     }
     }
 });
 });
 
 
+var InPlaceStringLiteral = TypeNarrowVariable.extend({
+    init: function(type){
+        TypeNarrowVariable.prototype.init.call(this, type, false, true);
+    },
+    idType: function(){return "string literal";}
+});
+
 var IdentdefInfo = Context.IdentdefInfo.extend({
 var IdentdefInfo = Context.IdentdefInfo.extend({
     init: function(id, exported, ro){
     init: function(id, exported, ro){
         Context.IdentdefInfo.prototype.init.call(this, id, exported);
         Context.IdentdefInfo.prototype.init.call(this, id, exported);
@@ -266,9 +264,10 @@ var TemplValueInit = Context.Chained.extend({
         this.__code = "var " + this.__id + " = ";
         this.__code = "var " + this.__id + " = ";
     },
     },
     handleExpression: function(e){
     handleExpression: function(e){
-        var v = new TypeNarrowVariable(e.type(), false, false);
-        this.__symbol = Symbol.makeSymbol(this.__id, v);
         var type = e.type();
         var type = e.type();
+        var v = Type.isString(type) ? new InPlaceStringLiteral(type) 
+                                    : new TypeNarrowVariable(type, false, false);
+        this.__symbol = Symbol.makeSymbol(this.__id, v);
         if (type instanceof Type.Record)
         if (type instanceof Type.Record)
             this.__code += this.language().rtl.clone(e.code());
             this.__code += this.language().rtl.clone(e.code());
         else if (type instanceof Type.Array){
         else if (type instanceof Type.Array){

+ 14 - 0
test/expected/eberon/in_place_variables.js

@@ -118,9 +118,23 @@ function varArgs(r/*VAR Derived*/, i/*VAR INTEGER*/, a/*ARRAY 10 OF INTEGER*/){
 	var v2 = i.get();
 	var v2 = i.get();
 	var v3 = RTL$.clone(a);
 	var v3 = RTL$.clone(a);
 }
 }
+
+function pChar(c/*CHAR*/){
+}
+
+function pCharArray(a/*ARRAY OF CHAR*/){
+}
+
+function pString(s/*STRING*/){
+}
 var v1 = 0;
 var v1 = 0;
 var v2 = 1.23;
 var v2 = 1.23;
 var v3 = "abc";
 var v3 = "abc";
+var vs = "\"";
+pChar(vs.charCodeAt(0));
+pChar(34);
+pCharArray(vs);
+pString(vs);
 var v4 = true;
 var v4 = true;
 var v5 = i;
 var v5 = i;
 var v6 = i + i | 0;
 var v6 = i + i | 0;

+ 16 - 0
test/input/eberon/in_place_variables.ob

@@ -36,10 +36,26 @@ BEGIN
     v3 <- a;
     v3 <- a;
 END varArgs;
 END varArgs;
 
 
+PROCEDURE pChar(c: CHAR);
+END pChar;
+
+PROCEDURE pCharArray(a: ARRAY OF CHAR);
+END pCharArray;
+
+PROCEDURE pString(s: STRING);
+END pString;
+
 BEGIN
 BEGIN
     v1 <- 0;
     v1 <- 0;
     v2 <- 1.23;
     v2 <- 1.23;
     v3 <- "abc";
     v3 <- "abc";
+    
+    vs <- 22X;
+    pChar(vs[0]);
+    pChar(vs);
+    pCharArray(vs);
+    pString(vs);
+    
     v4 <- TRUE;
     v4 <- TRUE;
     v5 <- i;
     v5 <- i;
     v6 <- i + i;
     v6 <- i + i;

+ 6 - 0
test/test_unit_eberon.js

@@ -424,6 +424,12 @@ exports.suite = {
         fail(["v <-", "initialization expression expected"],
         fail(["v <-", "initialization expression expected"],
              ["v <- void()", "procedure returning no result cannot be used in an expression"])
              ["v <- void()", "procedure returning no result cannot be used in an expression"])
         ),
         ),
+    "read-only if initialized with string literal": testWithContext(
+        context(grammar.declarationSequence, ""),
+        pass(),
+        fail(["PROCEDURE p(); BEGIN s <- \"abc\"; s := \"def\"; END p;", "cannot assign to string literal"],
+             ["PROCEDURE p(); BEGIN s <- \"abc\"; s[0] := \"d\"; END p;", "cannot assign to read-only variable"])
+        ),
     "scope": testWithContext(
     "scope": testWithContext(
         temporaryValues.context,
         temporaryValues.context,
         temporaryValues.passStatements(
         temporaryValues.passStatements(