Jesse McGrew avatar Jesse McGrew committed e2f5505

Fixed FLOAT lexing, array initializer parsing
Changed OR expr tree structure
Implemented FLOAT literal parsing
Various scope/symbol changes

Comments (0)

Files changed (6)

Rellor/Inform6.g3

 
 FLOAT
     :	'$' ('+'|'-')
-        (DEC_DIGIT+ ('.' DEC_DIGIT*) | '.' DEC_DIGIT+)
+        (DEC_DIGIT+ ('.' DEC_DIGIT*)? | '.' DEC_DIGIT+)
         (('e' | 'E') ('+'|'-')? DEC_DIGIT+)?
     ;
 
 
 public
 program
+scope { IScope globalScope; }
 scope Symbols, Quirks;
-@init { $Symbols::scope = new GlobalScope(); }
-    :	('#'!? directive ';'!)*
+@init { $program::globalScope = $Symbols::scope = new GlobalScope(); }
+    :	('#'!? directive[false] ';'!)*
     ;
 
-directive
+directive[bool nested]
 options { k=1; }
     :	include_directive
     |	version_directive
     |	release_directive
     |	serial_directive
     |	object_directive
+		{DefineSymbol($Symbols::scope, SymbolType.Object, $object_directive.name, $object_directive.tree);}
     |	routine_directive
+		{DefineSymbol($Symbols::scope, SymbolType.Routine, $routine_directive.name, $routine_directive.tree);}
     |	array_directive
+		{DefineSymbol($Symbols::scope, SymbolType.Array, $array_directive.name, $array_directive.tree);}
     |	global_directive
+		{DefineSymbol($Symbols::scope, SymbolType.GlobalVar, $global_directive.name, $global_directive.tree);}
     |	verb_directive
     ;
 
 include_directive
-    :	kINCLUDEci^ DQ_STRING
+    :	kINCLUDEci fn=DQ_STRING
     ;
 
 version_directive
     :	kSERIALci^ DQ_STRING
     ;
 
-object_directive
+object_directive returns [ITree name]
     :	kOBJECTci
         ARROW*
         n=id									// object identifier
+		{$name = $n.tree;}
         sn=DQ_STRING?							// textual short name
         p=id_not_seg?							// parent object
         object_parts
     :	{!IsKeyword((RellorToken)input.LT(1), CLASS, HAS, WITH, PRIVATE)}? id
     ;
     
-routine_directive returns [IScope routineScope, string name]
+routine_directive returns [IScope routineScope, ITree name]
 scope Symbols;
     :	LSQUARE n=id
 		{$routineScope = $Symbols::scope = new RoutineScope($Symbols[-1]::scope, $n.text);
-		 $name = $n.text;}
+		 $name = $n.tree;}
 		STAR?
-		(l+=id {DefineLocalVar(l.Tree, $routineScope);})* SEMI
+		(l+=id {DefineSymbol($routineScope, SymbolType.LocalVar, l.Tree);})* SEMI
 		switch_body
 		RSQUARE
-        -> ^(ROUTINE[$LSQUARE] $n ^(FLAGS STAR?)? ^(LOCALS $l*)? switch_body)
+		-> ^(ROUTINE[$LSQUARE] $n ^(FLAGS STAR?)? ^(LOCALS $l*)? switch_body)
     ;
 
-array_directive
-    :	kARRAYci^ id array_initializer
+array_directive returns [ITree name]
+    :	kARRAYci^ n=id array_initializer
+		{$name = $n.tree;}
     ;
 
 array_initializer
             )
             -> $k
         )
-        (	(number)=> number
-            -> $array_initializer UNINITIALIZED<node=NumberTree>[$number.start, ((NumberTree)$number.tree).Number]
+        (	(number ';')=> number
+            -> ^($array_initializer UNINITIALIZED<node=NumberTree>[$number.start, ((NumberTree)$number.tree).Number])
         |	arg_expr+
-            -> $array_initializer arg_expr+
+            -> ^($array_initializer arg_expr+)
         )
     ;
 
-global_directive
+global_directive returns [ITree name]
     :	kGLOBALci n=id
+		{$name = $n.tree;}
         (	/* nada */
             -> ^(kGLOBALci $n)
         |	EQ any_expr
     ;
 
 directive_stmt
-    :	'#'^ directive ';'!
+    :	'#'^ directive[true] ';'!
     ;
 
 null_stmt
     ;
 
 or_expr
-    :	add_expr (kOR^ add_expr)*
+    :	(	a+=add_expr
+			-> $a+
+		)
+		(	(kOR a+=add_expr)+
+			-> ^(kOR $a+)
+		)?
     ;
 
 add_expr

Rellor/Inform6.g3.parser.cs

 {
     partial class Inform6Parser
     {
-        public bool AllowExtensions { get; set; }
-        public bool BugCompatible { get; set; }
+        /*public bool AllowExtensions { get; set; }
+        public bool BugCompatible { get; set; }*/
 
         private static int ParseDecInt(string str)
         {
         private static int ParseFloat(string str)
         {
             Contract.Requires(str != null);
-            //XXX
-            throw new NotImplementedException();
+            
+            var value = float.Parse(str.Substring(1));
+            var bytes = BitConverter.GetBytes(value);
+
+            /*if (BitConverter.IsLittleEndian)
+            {
+                byte a = bytes[0], b = bytes[1], c = bytes[2], d = bytes[3];
+                bytes[0] = d;
+                bytes[1] = c;
+                bytes[2] = b;
+                bytes[3] = a;
+            }*/
+
+            return BitConverter.ToInt32(bytes, 0);
         }
 
         private static int ParseCharLiteral(string str)
             return tk == kw1 || tk == kw2 || tk == kw3 || tk == kw4;
         }
 
-        private void DefineLocalVar(ITree tree, IScope scope)
+        private void DefineSymbol(IScope scope, SymbolType type, ITree identifier, ITree value = null)
         {
-            Contract.Requires(tree is CommonTree);
             Contract.Requires(scope != null);
+            Contract.Requires(identifier is CommonTree);
 
-            IToken token = ((CommonTree)tree).Token;
+            IToken token = ((CommonTree)identifier).Token;
             Symbol symbol = new Symbol()
             {
                 Name = token.Text,
                 Defined = true,
-                Definition = tree,
-                Type = SymbolType.LocalVar,
+                Declaration = identifier,
+                Type = type,
+                Value = value,
             };
             scope.Define(token.Text, symbol);
         }

Rellor/Rellor.Core.csproj

   </ItemGroup>
   <ItemGroup>
     <Compile Include="RellorToken.cs" />
+    <Compile Include="RellorTokenStream.cs" />
     <Compile Include="RellorTreeAdaptor.cs" />
     <Compile Include="Scope.cs" />
     <Compile Include="Symbol.cs" />

Rellor/RellorTokenStream.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Antlr.Runtime;
+
+namespace Rellor.Core
+{
+    public class RellorTokenStream : CommonTokenStream
+    {
+        protected static RellorToken RellorEofToken = new RellorToken(TokenTypes.EndOfFile);
+
+        public RellorTokenStream()
+            : base() { }
+
+        public RellorTokenStream(ITokenSource source)
+            : base(source) { }
+
+        public RellorTokenStream(ITokenSource source, int channel)
+            : base(source, channel) { }
+
+        public override IToken LT(int k)
+        {
+            IToken token = base.LT(k);
+            return token.Type == TokenTypes.EndOfFile ? RellorEofToken : token;
+        }
+    }
+}
         GlobalVar,
         [Description("local variable")]
         LocalVar,
+        Array,
         Label,
     }
 
 
         public string Name { get; set; }
         public SymbolType Type { get; set; }
-        public ITree Definition { get; set; }
+        public ITree Declaration { get; set; }
         public bool Defined { get; set; }
         public bool Referenced { get; set; }
         public object Value { get; set; }

RellorC/Program.cs

         {
             const string SText =
 @"
+Array foo -> 1 2 3;
+Array bar --> 5;
+Array baz --> (5);
+[ Main;
+    x == 1 or 2 or 3;
+    !while (x < $+1.5e6) { @""fadd"" x $+5 -> sp ?~foo; }
+    !do { j++, k++; } until (j ~= k or 5 or 3 or 4+1 or 0);
+];";
+/*@"
 [ Main;
     switch (foo) {
         1: print 1;
         7, 9 to 10: print 7910;
         default: print ""default"";
     }
-];";
+];";*/
 /*@"
 [ Main;
     for (::) 0; for (:;) 0;
 ";*/
 
             Inform6Lexer lexer = new Inform6Lexer(new ANTLRStringStream(SText));
-            Inform6Parser parser = new Inform6Parser(new CommonTokenStream(lexer));
+            Inform6Parser parser = new Inform6Parser(new RellorTokenStream(lexer));
             var parserResult = parser.program();
 
             Console.WriteLine("Parser result:");
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.