Ver Fonte

v3 step 1.3 — modern tokens: 0x-only hex, a-f, real exponents, // comments (6/6 tests green)

Eric Streit há 3 semanas atrás
pai
commit
db63dc943f
4 ficheiros alterados com 16 adições e 11 exclusões
  1. 8 5
      compiler/src/M2.atg
  2. 1 1
      compiler/src/M2.lst
  3. 5 4
      compiler/src/QbeGen.mod
  4. 2 1
      compiler/tests/t_arith.mod

+ 8 - 5
compiler/src/M2.atg

@@ -19,22 +19,25 @@ IMPORT SymTab, QbeGen;
 
 CHARACTERS
   eol      = CHR(13) .
+  lf       = CHR(10) .
   letter   = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
   digit    = "0123456789" .
-  hexDigit = digit + "ABCDEF" .
+  hexDigit = digit + "ABCDEFabcdef" .
   noQuote1 = ANY - "'" - eol .
   noQuote2 = ANY - '"' - eol .
 
 IGNORE CHR(9) .. CHR(13)
 
-COMMENTS
-  FROM "(*" TO "*)" NESTED
+COMMENTS FROM "(*" TO "*)" NESTED
+COMMENTS FROM "//" TO lf
 
 TOKENS
   ident   = letter { letter | digit } .
   integer = digit { digit }
-          | digit { hexDigit } "H" .
-  real    = digit { digit } "." { digit } .
+          | "0x" hexDigit { hexDigit }
+          | "0X" hexDigit { hexDigit } .
+  real    = digit { digit } "." { digit }
+            [ ( "E" | "e" ) [ "+" | "-" ] digit { digit } ] .
   string  = "'" { noQuote1 } "'"
           | '"' { noQuote2 } '"' .
 

+ 1 - 1
compiler/src/M2.lst

@@ -19,6 +19,6 @@ Statistics:
   nr of symbolnodes:      41 (limit   500)
   nr of graphnodes:      108 (limit  1500)
   nr of conditionsets:     1 (limit   100)
-  nr of charactersets:     8 (limit   250)
+  nr of charactersets:    11 (limit   250)
 
 

+ 5 - 4
compiler/src/QbeGen.mod

@@ -113,14 +113,15 @@ PROCEDURE HexVal (ch: CHAR): INTEGER;
   END HexVal;
 
 PROCEDURE NormInt (s: ARRAY OF CHAR; VAR d: QVal);
-(* "0FFH" -> "255", plain decimals are copied through. *)
+(* "0xFF" -> "255", plain decimals are copied through. *)
   VAR n, i : CARDINAL;
     v : INTEGER;
   BEGIN
     n := Len(s);
-    IF (n > 0) & ((s[n - 1] = "H") OR (s[n - 1] = "h")) THEN
-      v := 0; i := 0;
-      WHILE i < n - 1 DO v := v * 16 + HexVal(s[i]); INC(i) END;
+    IF (n > 2) & (s[0] = "0")
+       & ((s[1] = "x") OR (s[1] = "X")) THEN
+      v := 0; i := 2;
+      WHILE i < n DO v := v * 16 + HexVal(s[i]); INC(i) END;
       IntStr(v, d)
     ELSE
       Cpy(d, s)

+ 2 - 1
compiler/tests/t_arith.mod

@@ -1,6 +1,7 @@
 MODULE TArith;
+// Step-1 arithmetic: precedence, DIV truncation, 0x hex, CONST loads.
 CONST C = 10;
 VAR ExitCode : INTEGER;
 BEGIN
-  ExitCode := C + 3 * 4 - 10 DIV 3 + 10H - C
+  ExitCode := C + 3 * 4 - 10 DIV 3 + 0x10 - C
 END TArith.