]> khamidou — GitHub mirrors - tiny.pas.git/commitdiff
add a semi-functional lexer.
authorKarim Hamidou <karim@macbook.home>
Tue, 29 Dec 2015 18:48:22 +0000 (19:48 +0100)
committerKarim Hamidou <karim@macbook.home>
Tue, 29 Dec 2015 18:48:22 +0000 (19:48 +0100)
Makefile
test.pas
tiny.pas

index 986438dfc88be8e0863ecc13018e6d86c86dde57..c41ecb06c0d294f8e3e7615db1a6129101c90c62 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,13 +1,6 @@
-all: tiny.pas lexer.pas lexlib.o
-       fpc -Mtp lexlib.pas tiny.pas
-
-lexlib.o: lexlib.pas
-       fpc -Mtp lexlib.pas
-
-lexer.pas: lexer.l
-       plex lexer.l
+default: tiny.pas
+       fpc -Mtp tiny.pas
 
 clean:
        rm *.o
-       rm lexer.pas
        rm tiny
index 8df2f75b713a26c05e2491102a52f02a53747adc..d7cb8bd9fb927c8fb4e9e0a91c7736181c3d95b3 100644 (file)
--- a/test.pas
+++ b/test.pas
@@ -1,2 +1,15 @@
 { Test program }
 program test;
+
+uses
+    SysUtils;
+
+var
+    c: char;
+begin
+    c := 'a';
+    if (c = 'b') or (c = 'c') then
+    begin
+        Writeln('Wot?');
+    end;
+end.
index 630fc994f94ca49058fab9f9754e30306de33937..d1b0dc9056318ff5139f2bd7fcf4e8a224bddb56 100644 (file)
--- a/tiny.pas
+++ b/tiny.pas
@@ -5,7 +5,7 @@ Compiler for tinypas. Everything is rolled in a single file.
 program tinypascal;
 
 uses
-    SysUtils, LexLib;
+    SysUtils;
 
 const
     _PROGRAM = 1;
@@ -17,6 +17,19 @@ const
     _RBRACE = 7;
     _IF = 8;
     _SEMICOLON = 9;
+    _BEGIN = 10;
+    _END = 11;
+    _THEN = 12;
+    _ELSE = 13;
+    _DOT = 14;
+    _AND = 15;
+    _OR = 16;
+    _PLUS = 17;
+    _MINUS = 18;
+    _MULT = 19;
+    _DIV = 20;
+    _FUNCTION = 21;
+    _PROCEDURE = 22;
 
 var
     yyinput: Text;
@@ -30,47 +43,9 @@ begin
     strlen := length(s)
 end;
 
-function yylex: integer;
-var
-    c: char;
-    i: integer;
-    s: String;
-begin
-    s := '';
-    i := 0;
-
-    { Ignore white space }
-    repeat
-        Read(yyinput, c);
-    until (c = ' ' or c = '\t' or c = '\n');
-
-    if c = '{' then
-        repeat
-            Read(yyinput, c);
-        until (c = '}' or c = '\t' or c = '\n');
-    end
-
-    repeat
-        Read(yyinput, c)
-
-
-    repeat
-        Read(yyinput, c);
-        i := i + 1
-        s[i] = c;
-    until (c = ' ' or c = '\t' or c = '\n');
-
-    Write(c);
-    yylex := _PROGRAM;
-end;
-
 procedure error(s: String; s2: String);
 begin
-    Write('Error at line ');
-    Write(yylineno);
-    Write(':');
-    Write(yycolno);
-    Write(' ');
+    Write('Compilation error: ');
     Write(s);
 
     if s2 <> '' then
@@ -81,34 +56,95 @@ begin
     Writeln('');
 end;
 
-function strip_whitespace: integer;
+{
+  As far as lexers go, this one is pretty slow because
+  it reads everything byte-by-byte.
+}
+function yylex: integer;
 var
-    token: integer;
+    c: Char;
+    fpos: Integer;
 begin
-    repeat
-        token := yylex();
-    until(token <> 3);
+    yytext := '';
+    Read(yyinput, c);
 
-    strip_whitespace := token
+    while True do
+    begin
+        { Ignore white space and comments }
+        if c = '{' then
+            begin
+                repeat
+                    Read(yyinput, c);
+                until (c = '}');
+
+                Read(yyinput, c);
+                continue;
+            end
+        { #9 and #10 are the Pascal notation for tab and \n, respectively. }
+        else if (c = ' ') or (c = #10) or (c = #09) then
+            begin
+                repeat
+                    Read(yyinput, c);
+                until (c <> ' ') and (c <> #10) and (c <> #09);
+                continue;
+            end
+        else
+            break;
+
+        Read(yyinput, c);
+    end;
+
+    if c = ';' then
+        exit(_SEMICOLON)
+    else if c = '.' then
+        exit(_DOT);
+    if ((c >= 'a') and (c <= 'z')) or ((c >= 'A') and (c <= 'Z'))  then
+        begin
+            repeat
+                yytext := yytext + c;
+                Read(yyinput, c);
+            until (c = ' ') or (c = '\n') or (c = '\t') or (c = ';') or (c = '.');
+
+            { We've read one char too much --- rewind the file position
+              one character back.}
+            fpos := Filepos(yyinput);
+            Seek(yyinput, fpos - 1);
+
+            { Convert to lowercase for convenience }
+            yytext := Lowercase(yytext);
+
+            if CompareText(yytext, 'program') = 0 then
+                exit(_PROGRAM)
+            else if CompareText(yytext, 'uses') = 0 then
+                exit(_USES)
+            else if CompareText(yytext, 'if') = 0 then
+                exit(_IF)
+            else
+                exit(_IDENTIFIER);
+        end;
 end;
 
 procedure pascal_program;
 var
     token: integer;
 begin
-    token := strip_whitespace();
+    token := yylex();
     if token <> _PROGRAM then
-    begin
-        error('Expecting PROGRAM statement, got: ', yytext)
-    end;
+        error('expecting PROGRAM statement, got: ', yytext);
+
+    token := yylex();
+    if token <> _IDENTIFIER then
+        error('expecting identifier, got: ', yytext);
+
+    token := yylex();
+    if token <> _SEMICOLON then
+        error('expecting semicolon, got', yytext);
+
 end;
 
 begin
     assign(yyinput, 'test.pas');
-    assign(yyoutput, '');
-    reset(yyinput); rewrite(yyoutput);
-    yylineno := 0;
-    yyclear;
+    reset(yyinput);
 
     pascal_program();
 end.