]> khamidou — GitHub mirrors - tiny.pas.git/commitdiff
initial commit
authorKarim Hamidou <khamidou@gmail.com>
Sun, 27 Dec 2015 16:26:36 +0000 (17:26 +0100)
committerKarim Hamidou <khamidou@gmail.com>
Sun, 27 Dec 2015 16:26:36 +0000 (17:26 +0100)
Makefile [new file with mode: 0644]
hello.pas [new file with mode: 0644]
lexer.l [new file with mode: 0644]
lexer.pas [new file with mode: 0644]
lexlib.pas [new file with mode: 0644]
test.asm [new file with mode: 0644]
test.pas [new file with mode: 0644]
tiny.pas [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..986438d
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,13 @@
+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
+
+clean:
+       rm *.o
+       rm lexer.pas
+       rm tiny
diff --git a/hello.pas b/hello.pas
new file mode 100644 (file)
index 0000000..8ddcd50
--- /dev/null
+++ b/hello.pas
@@ -0,0 +1,77 @@
+%{
+{tinypascal --- a tiny, tiny pascal compiler}
+program tinypascal;
+
+uses
+    SysUtils;
+
+type
+    Token = (_PROGRAM, _USES, _EOF);
+var
+    inputFile: TextFile;
+
+procedure openFile(fileName: String);
+begin
+    AssignFile(inputFile, fileName);
+    reset(inputFile);
+end;
+
+function strlen(s: String): integer
+    strlen = byte(s[0])
+end
+
+function getch(): char;
+var
+    c: char;
+begin
+    read(inputFile, c);
+    getch:= c;
+end;
+
+function lexer(): Token;
+var
+    c: char;
+begin
+    if eof(inputFile) then
+    begin
+        lexer := _EOF;
+    end;
+
+    yytext := ''
+
+    (Skip spaces)
+    c := getch()
+    while c = ' ' or c = '\t' or c = '\n' do
+    begin
+        c := getch()
+    end;
+
+    if c = ';'
+        lexer := _SEMICOLON;
+    while c <> ' ' and c <> '\t' and c <> '\n' do
+    begin
+
+    end;
+
+    lexer := _PROGRAM;
+end;
+
+var
+    ret: integer;
+begin
+    openFile('hello.pas');
+    writeln(lexer());
+end.
+%}
+
+%%
+
+[ \t\n]+    return(_WHITE_SPACE);
+number [0-9]+   return(_NUMBER);
+alpha [a-zA-Z_]+    return(_IDENTIFIER);
+"{"     return(_LBRACE);
+"}"     return(_RBRACE);
+"program"   return(_PROGRAM);
+"if"    return(_IF);
+
+.return(ILLEGAL);
diff --git a/lexer.l b/lexer.l
new file mode 100644 (file)
index 0000000..b2dedac
--- /dev/null
+++ b/lexer.l
@@ -0,0 +1,9 @@
+%%
+
+[ \t\n]+        return(_WHITE_SPACE);
+[0-9]+          return(_NUMBER);
+'program'       return(_PROGRAM);
+'\{'            return(_LBRACE);
+'\}'            return(_RBRACE);
+'if'            return(_IF);
+;               return(_SEMICOLON);
diff --git a/lexer.pas b/lexer.pas
new file mode 100644 (file)
index 0000000..f69b7ea
--- /dev/null
+++ b/lexer.pas
@@ -0,0 +1,359 @@
+
+(* lexical analyzer template (TP Lex V3.0), V1.0 3-2-91 AG *)
+
+(* global definitions: *)
+
+function yylex : Integer;
+
+procedure yyaction ( yyruleno : Integer );
+  (* local definitions: *)
+
+begin
+  (* actions: *)
+  case yyruleno of
+  1:
+                return(_WHITE_SPACE);
+  2:
+                return(_NUMBER);
+  3:
+                return(_PROGRAM);
+  4:
+                return(_LBRACE);
+  5:
+                return(_RBRACE);
+  6:
+                return(_IF);
+  7:
+                return(_SEMICOLON);
+  end;
+end(*yyaction*);
+
+(* DFA table: *)
+
+type YYTRec = record
+                cc : set of Char;
+                s  : Integer;
+              end;
+
+const
+
+yynmarks   = 7;
+yynmatches = 7;
+yyntrans   = 25;
+yynstates  = 21;
+
+yyk : array [1..yynmarks] of Integer = (
+  { 0: }
+  { 1: }
+  { 2: }
+  1,
+  { 3: }
+  2,
+  { 4: }
+  { 5: }
+  7,
+  { 6: }
+  { 7: }
+  { 8: }
+  { 9: }
+  { 10: }
+  { 11: }
+  4,
+  { 12: }
+  5,
+  { 13: }
+  { 14: }
+  { 15: }
+  6,
+  { 16: }
+  { 17: }
+  { 18: }
+  { 19: }
+  { 20: }
+  3
+);
+
+yym : array [1..yynmatches] of Integer = (
+{ 0: }
+{ 1: }
+{ 2: }
+  1,
+{ 3: }
+  2,
+{ 4: }
+{ 5: }
+  7,
+{ 6: }
+{ 7: }
+{ 8: }
+{ 9: }
+{ 10: }
+{ 11: }
+  4,
+{ 12: }
+  5,
+{ 13: }
+{ 14: }
+{ 15: }
+  6,
+{ 16: }
+{ 17: }
+{ 18: }
+{ 19: }
+{ 20: }
+  3
+);
+
+yyt : array [1..yyntrans] of YYTrec = (
+{ 0: }
+  ( cc: [ #9,#10,' ' ]; s: 2),
+  ( cc: [ '''' ]; s: 4),
+  ( cc: [ '0'..'9' ]; s: 3),
+  ( cc: [ ';' ]; s: 5),
+{ 1: }
+  ( cc: [ #9,#10,' ' ]; s: 2),
+  ( cc: [ '''' ]; s: 4),
+  ( cc: [ '0'..'9' ]; s: 3),
+  ( cc: [ ';' ]; s: 5),
+{ 2: }
+  ( cc: [ #9,#10,' ' ]; s: 2),
+{ 3: }
+  ( cc: [ '0'..'9' ]; s: 3),
+{ 4: }
+  ( cc: [ 'i' ]; s: 9),
+  ( cc: [ 'p' ]; s: 6),
+  ( cc: [ '{' ]; s: 7),
+  ( cc: [ '}' ]; s: 8),
+{ 5: }
+{ 6: }
+  ( cc: [ 'r' ]; s: 10),
+{ 7: }
+  ( cc: [ '''' ]; s: 11),
+{ 8: }
+  ( cc: [ '''' ]; s: 12),
+{ 9: }
+  ( cc: [ 'f' ]; s: 13),
+{ 10: }
+  ( cc: [ 'o' ]; s: 14),
+{ 11: }
+{ 12: }
+{ 13: }
+  ( cc: [ '''' ]; s: 15),
+{ 14: }
+  ( cc: [ 'g' ]; s: 16),
+{ 15: }
+{ 16: }
+  ( cc: [ 'r' ]; s: 17),
+{ 17: }
+  ( cc: [ 'a' ]; s: 18),
+{ 18: }
+  ( cc: [ 'm' ]; s: 19),
+{ 19: }
+  ( cc: [ '''' ]; s: 20)
+{ 20: }
+);
+
+yykl : array [0..yynstates-1] of Integer = (
+{ 0: } 1,
+{ 1: } 1,
+{ 2: } 1,
+{ 3: } 2,
+{ 4: } 3,
+{ 5: } 3,
+{ 6: } 4,
+{ 7: } 4,
+{ 8: } 4,
+{ 9: } 4,
+{ 10: } 4,
+{ 11: } 4,
+{ 12: } 5,
+{ 13: } 6,
+{ 14: } 6,
+{ 15: } 6,
+{ 16: } 7,
+{ 17: } 7,
+{ 18: } 7,
+{ 19: } 7,
+{ 20: } 7
+);
+
+yykh : array [0..yynstates-1] of Integer = (
+{ 0: } 0,
+{ 1: } 0,
+{ 2: } 1,
+{ 3: } 2,
+{ 4: } 2,
+{ 5: } 3,
+{ 6: } 3,
+{ 7: } 3,
+{ 8: } 3,
+{ 9: } 3,
+{ 10: } 3,
+{ 11: } 4,
+{ 12: } 5,
+{ 13: } 5,
+{ 14: } 5,
+{ 15: } 6,
+{ 16: } 6,
+{ 17: } 6,
+{ 18: } 6,
+{ 19: } 6,
+{ 20: } 7
+);
+
+yyml : array [0..yynstates-1] of Integer = (
+{ 0: } 1,
+{ 1: } 1,
+{ 2: } 1,
+{ 3: } 2,
+{ 4: } 3,
+{ 5: } 3,
+{ 6: } 4,
+{ 7: } 4,
+{ 8: } 4,
+{ 9: } 4,
+{ 10: } 4,
+{ 11: } 4,
+{ 12: } 5,
+{ 13: } 6,
+{ 14: } 6,
+{ 15: } 6,
+{ 16: } 7,
+{ 17: } 7,
+{ 18: } 7,
+{ 19: } 7,
+{ 20: } 7
+);
+
+yymh : array [0..yynstates-1] of Integer = (
+{ 0: } 0,
+{ 1: } 0,
+{ 2: } 1,
+{ 3: } 2,
+{ 4: } 2,
+{ 5: } 3,
+{ 6: } 3,
+{ 7: } 3,
+{ 8: } 3,
+{ 9: } 3,
+{ 10: } 3,
+{ 11: } 4,
+{ 12: } 5,
+{ 13: } 5,
+{ 14: } 5,
+{ 15: } 6,
+{ 16: } 6,
+{ 17: } 6,
+{ 18: } 6,
+{ 19: } 6,
+{ 20: } 7
+);
+
+yytl : array [0..yynstates-1] of Integer = (
+{ 0: } 1,
+{ 1: } 5,
+{ 2: } 9,
+{ 3: } 10,
+{ 4: } 11,
+{ 5: } 15,
+{ 6: } 15,
+{ 7: } 16,
+{ 8: } 17,
+{ 9: } 18,
+{ 10: } 19,
+{ 11: } 20,
+{ 12: } 20,
+{ 13: } 20,
+{ 14: } 21,
+{ 15: } 22,
+{ 16: } 22,
+{ 17: } 23,
+{ 18: } 24,
+{ 19: } 25,
+{ 20: } 26
+);
+
+yyth : array [0..yynstates-1] of Integer = (
+{ 0: } 4,
+{ 1: } 8,
+{ 2: } 9,
+{ 3: } 10,
+{ 4: } 14,
+{ 5: } 14,
+{ 6: } 15,
+{ 7: } 16,
+{ 8: } 17,
+{ 9: } 18,
+{ 10: } 19,
+{ 11: } 19,
+{ 12: } 19,
+{ 13: } 20,
+{ 14: } 21,
+{ 15: } 21,
+{ 16: } 22,
+{ 17: } 23,
+{ 18: } 24,
+{ 19: } 25,
+{ 20: } 25
+);
+
+
+var yyn : Integer;
+
+label start, scan, action;
+
+begin
+
+start:
+
+  (* initialize: *)
+
+  yynew;
+
+scan:
+
+  (* mark positions and matches: *)
+
+  for yyn := yykl[yystate] to     yykh[yystate] do yymark(yyk[yyn]);
+  for yyn := yymh[yystate] downto yyml[yystate] do yymatch(yym[yyn]);
+
+  if yytl[yystate]>yyth[yystate] then goto action; (* dead state *)
+
+  (* get next character: *)
+
+  yyscan;
+
+  (* determine action: *)
+
+  yyn := yytl[yystate];
+  while (yyn<=yyth[yystate]) and not (yyactchar in yyt[yyn].cc) do inc(yyn);
+  if yyn>yyth[yystate] then goto action;
+    (* no transition on yyactchar in this state *)
+
+  (* switch to new state: *)
+
+  yystate := yyt[yyn].s;
+
+  goto scan;
+
+action:
+
+  (* execute action: *)
+
+  if yyfind(yyrule) then
+    begin
+      yyaction(yyrule);
+      if yyreject then goto action;
+    end
+  else if not yydefault and yywrap() then
+    begin
+      yyclear;
+      return(0);
+    end;
+
+  if not yydone then goto start;
+
+  yylex := yyretval;
+
+end(*yylex*);
+
diff --git a/lexlib.pas b/lexlib.pas
new file mode 100644 (file)
index 0000000..4d0925f
--- /dev/null
@@ -0,0 +1,401 @@
+
+{$I-}
+
+unit LexLib;
+
+(* Standard Lex library unit for TP Lex Version 3.0.
+   2-11-91 AG *)
+
+interface
+
+(* The Lex library unit supplies a collection of variables and routines
+   needed by the lexical analyzer routine yylex and application programs
+   using Lex-generated lexical analyzers. It also provides access to the
+   input/output streams used by the lexical analyzer and the text of the
+   matched string, and provides some utility functions which may be used
+   in actions.
+
+   This `standard' version of the LexLib unit is used to implement lexical
+   analyzers which read from and write to MS-DOS files (using standard input
+   and output, by default). It is suitable for many standard applications
+   for lexical analyzers, such as text conversion tools or compilers.
+
+   However, you may create your own version of the LexLib unit, tailored to
+   your target applications. In particular, you may wish to provide another
+   set of I/O functions, e.g., if you want to read from or write to memory
+   instead to files, or want to use different file types. *)
+
+(* Variables:
+
+   The variable yytext contains the current match, yyleng its length.
+   The variable yyline contains the current input line, and yylineno and
+   yycolno denote the current input position (line, column). These values
+   are often used in giving error diagnostics (however, they will only be
+   meaningful if there is no rescanning across line ends).
+
+   The variables yyinput and yyoutput are the text files which are used
+   by the lexical analyzer. By default, they are assigned to standard
+   input and output, but you may change these assignments to fit your
+   target application (use the Turbo Pascal standard routines assign,
+   reset, and rewrite for this purpose). *)
+
+var
+
+yyinput, yyoutput : Text;        (* input and output file *)
+yyline            : String;      (* current input line *)
+yylineno, yycolno : Integer;     (* current input position *)
+yytext            : String;      (* matched text (should be considered r/o) *)
+yyleng            : Byte         (* length of matched text *)
+  absolute yytext;
+
+(* I/O routines:
+
+   The following routines get_char, unget_char and put_char are used to
+   implement access to the input and output files. Since \n (newline) for
+   Lex means line end, the I/O routines have to translate MS-DOS line ends
+   (carriage-return/line-feed) into newline characters and vice versa. Input
+   is buffered to allow rescanning text (via unput_char).
+
+   The input buffer holds the text of the line to be scanned. When the input
+   buffer empties, a new line is obtained from the input stream. Characters
+   can be returned to the input buffer by calls to unget_char. At end-of-
+   file a null character is returned.
+
+   The input routines also keep track of the input position and set the
+   yyline, yylineno, yycolno variables accordingly.
+
+   Since the rest of the Lex library only depends on these three routines
+   (there are no direct references to the yyinput and yyoutput files or
+   to the input buffer), you can easily replace get_char, unget_char and
+   put_char by another suitable set of routines, e.g. if you want to read
+   from/write to memory, etc. *)
+
+function get_char : Char;
+  (* obtain one character from the input file (null character at end-of-
+     file) *)
+
+procedure unget_char ( c : Char );
+  (* return one character to the input file to be reread in subsequent calls
+     to get_char *)
+
+procedure put_char ( c : Char );
+  (* write one character to the output file *)
+
+(* Utility routines: *)
+
+procedure echo;
+  (* echoes the current match to the output stream *)
+
+procedure yymore;
+  (* append the next match to the current one *)
+
+procedure yyless ( n : Integer );
+  (* truncate yytext to size n and return the remaining characters to the
+     input stream *)
+
+procedure reject;
+  (* reject the current match and execute the next one *)
+
+  (* reject does not actually cause the input to be rescanned; instead,
+     internal state information is used to find the next match. Hence
+     you should not try to modify the input stream or the yytext variable
+     when rejecting a match. *)
+
+procedure return ( n : Integer );
+procedure returnc ( c : Char );
+  (* sets the return value of yylex *)
+
+procedure start ( state : Integer );
+  (* puts the lexical analyzer in the given start state; state=0 denotes
+     the default start state, other values are user-defined *)
+
+function yywrap : Boolean;
+
+  (* The default yywrap routine supplied here closes input and output files
+     and returns true (causing yylex to terminate). *)
+
+(* The following are the internal data structures and routines used by the
+   lexical analyzer routine yylex; they should not be used directly. *)
+
+var
+
+yystate    : Integer; (* current state of lexical analyzer *)
+yyactchar  : Char;    (* current character *)
+yylastchar : Char;    (* last matched character (#0 if none) *)
+yyrule     : Integer; (* matched rule *)
+yyreject   : Boolean; (* current match rejected? *)
+yydone     : Boolean; (* yylex return value set? *)
+yyretval   : Integer; (* yylex return value *)
+
+procedure yynew;
+  (* starts next match; initializes state information of the lexical
+     analyzer *)
+
+procedure yyscan;
+  (* gets next character from the input stream and updates yytext and
+     yyactchar accordingly *)
+
+procedure yymark ( n : Integer );
+  (* marks position for rule no. n *)
+
+procedure yymatch ( n : Integer );
+  (* declares a match for rule number n *)
+
+function yyfind ( var n : Integer ) : Boolean;
+  (* finds the last match and the corresponding marked position and adjusts
+     the matched string accordingly; returns:
+     - true if a rule has been matched, false otherwise
+     - n: the number of the matched rule *)
+
+function yydefault : Boolean;
+  (* executes the default action (copy character); returns true unless
+     at end-of-file *)
+
+procedure yyclear;
+  (* reinitializes state information after lexical analysis has been
+     finished *)
+
+implementation
+
+procedure fatal ( msg : String );
+  (* writes a fatal error message and halts program *)
+  begin
+    writeln('LexLib: ', msg);
+    halt(1);
+  end(*fatal*);
+
+(* I/O routines: *)
+
+const nl = #10;  (* newline character *)
+
+const max_chars = 2048;
+
+var
+
+bufptr : Integer;
+buf    : array [1..max_chars] of Char;
+
+function get_char : Char;
+  var i : Integer;
+  begin
+    if (bufptr=0) and not eof(yyinput) then
+      begin
+        readln(yyinput, yyline);
+        inc(yylineno); yycolno := 1;
+        buf[1] := nl;
+        for i := 1 to length(yyline) do
+          buf[i+1] := yyline[length(yyline)-i+1];
+        inc(bufptr, length(yyline)+1);
+      end;
+    if bufptr>0 then
+      begin
+        get_char := buf[bufptr];
+        dec(bufptr);
+        inc(yycolno);
+      end
+    else
+      get_char := #0;
+  end(*get_char*);
+
+procedure unget_char ( c : Char );
+  begin
+    if bufptr=max_chars then fatal('input buffer overflow');
+    inc(bufptr);
+    dec(yycolno);
+    buf[bufptr] := c;
+  end(*unget_char*);
+
+procedure put_char ( c : Char );
+  begin
+    if c=#0 then
+      { ignore }
+    else if c=nl then
+      writeln(yyoutput)
+    else
+      write(yyoutput, c)
+  end(*put_char*);
+
+(* Variables:
+
+   Some state information is maintained to keep track with calls to yymore,
+   yyless, reject, start and yymatch/yymark, and to initialize state
+   information used by the lexical analyzer.
+   - yystext: contains the initial contents of the yytext variable; this
+     will be the empty string, unless yymore is called which sets yystext
+     to the current yytext
+   - yysstate: start state of lexical analyzer (set to 0 during
+     initialization, and modified in calls to the start routine)
+   - yylstate: line state information (1 if at beginning of line, 0
+     otherwise)
+   - yystack: stack containing matched rules; yymatches contains the number of
+     matches
+   - yypos: for each rule the last marked position (yymark); zeroed when rule
+     has already been considered
+   - yysleng: copy of the original yyleng used to restore state information
+     when reject is used *)
+
+const
+
+max_matches = 1024;
+max_rules   = 256;
+
+var
+
+yystext            : String;
+yysstate, yylstate : Integer;
+yymatches          : Integer;
+yystack            : array [1..max_matches] of Integer;
+yypos              : array [1..max_rules] of Integer;
+yysleng            : Byte;
+
+(* Utilities: *)
+
+procedure echo;
+  var i : Integer;
+  begin
+    for i := 1 to yyleng do
+      put_char(yytext[i])
+  end(*echo*);
+
+procedure yymore;
+  begin
+    yystext := yytext;
+  end(*yymore*);
+
+procedure yyless ( n : Integer );
+  var i : Integer;
+  begin
+    for i := yyleng downto n+1 do
+      unget_char(yytext[i]);
+    yyleng := n;
+  end(*yyless*);
+
+procedure reject;
+  var i : Integer;
+  begin
+    yyreject := true;
+    for i := yyleng+1 to yysleng do
+      yytext := yytext+get_char;
+    dec(yymatches);
+  end(*reject*);
+
+procedure return ( n : Integer );
+  begin
+    yyretval := n;
+    yydone := true;
+  end(*return*);
+
+procedure returnc ( c : Char );
+  begin
+    yyretval := ord(c);
+    yydone := true;
+  end(*returnc*);
+
+procedure start ( state : Integer );
+  begin
+    yysstate := state;
+  end(*start*);
+
+(* yywrap: *)
+
+function lexlib_yywrap : Boolean;
+  begin
+    close(yyinput);
+    close(yyoutput);
+    lexlib_yywrap := true;
+  end(*yywrap*);
+
+(* Internal routines: *)
+
+procedure yynew;
+  begin
+    if yylastchar<>#0 then
+      if yylastchar=nl then
+        yylstate := 1
+      else
+        yylstate := 0;
+    yystate := yysstate+yylstate;
+    yytext  := yystext;
+    yystext := '';
+    yymatches := 0;
+    yydone := false;
+  end(*yynew*);
+
+procedure yyscan;
+  begin
+    if yyleng=255 then fatal('yytext overflow');
+    yyactchar := get_char;
+    inc(yyleng);
+    yytext[yyleng] := yyactchar;
+  end(*yyscan*);
+
+procedure yymark ( n : Integer );
+  begin
+    if n>max_rules then fatal('too many rules');
+    yypos[n] := yyleng;
+  end(*yymark*);
+
+procedure yymatch ( n : Integer );
+  begin
+    inc(yymatches);
+    if yymatches>max_matches then fatal('match stack overflow');
+    yystack[yymatches] := n;
+  end(*yymatch*);
+
+function yyfind ( var n : Integer ) : Boolean;
+  begin
+    yyreject := false;
+    while (yymatches>0) and (yypos[yystack[yymatches]]=0) do
+      dec(yymatches);
+    if yymatches>0 then
+      begin
+        yysleng := yyleng;
+        n       := yystack[yymatches];
+        yyless(yypos[n]);
+        yypos[n] := 0;
+        if yyleng>0 then
+          yylastchar := yytext[yyleng]
+        else
+          yylastchar := #0;
+        yyfind := true;
+      end
+    else
+      begin
+        yyless(0);
+        yylastchar := #0;
+        yyfind := false;
+      end
+  end(*yyfind*);
+
+function yywrap: Boolean;
+begin
+    yywrap := true
+end;
+
+function yydefault : Boolean;
+  begin
+    yyreject := false;
+    yyactchar := get_char;
+    if yyactchar<>#0 then
+      begin
+        put_char(yyactchar);
+        yydefault := true;
+      end
+    else
+      begin
+        yylstate := 1;
+        yydefault := false;
+      end;
+    yylastchar := yyactchar;
+  end(*yydefault*);
+
+procedure yyclear;
+  begin
+    bufptr := 0;
+    yysstate := 0;
+    yylstate := 1;
+    yylastchar := #0;
+    yytext := '';
+    yystext := '';
+  end(*yyclear*);
+end.
diff --git a/test.asm b/test.asm
new file mode 100644 (file)
index 0000000..9ea2710
--- /dev/null
+++ b/test.asm
@@ -0,0 +1,8 @@
+# Simple exit program
+.section __TEXT,__text
+
+.globl start
+start:
+  movl $0x2000001, %eax # system call $1 with $0x2000000 offset
+  movl $0, %ebx         # set the exit code to be $0
+  syscall
diff --git a/test.pas b/test.pas
new file mode 100644 (file)
index 0000000..8df2f75
--- /dev/null
+++ b/test.pas
@@ -0,0 +1,2 @@
+{ Test program }
+program test;
diff --git a/tiny.pas b/tiny.pas
new file mode 100644 (file)
index 0000000..630fc99
--- /dev/null
+++ b/tiny.pas
@@ -0,0 +1,114 @@
+{
+Compiler for tinypas. Everything is rolled in a single file.
+}
+
+program tinypascal;
+
+uses
+    SysUtils, LexLib;
+
+const
+    _PROGRAM = 1;
+    _USES = 2;
+    _WHITE_SPACE = 3;
+    _NUMBER = 4;
+    _IDENTIFIER = 5;
+    _LBRACE = 6;
+    _RBRACE = 7;
+    _IF = 8;
+    _SEMICOLON = 9;
+
+var
+    yyinput: Text;
+    yytext: String;
+
+var
+    tok: integer;
+
+function strlen(s: String): integer;
+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(s);
+
+    if s2 <> '' then
+    begin
+        Write(s2)
+    end;
+
+    Writeln('');
+end;
+
+function strip_whitespace: integer;
+var
+    token: integer;
+begin
+    repeat
+        token := yylex();
+    until(token <> 3);
+
+    strip_whitespace := token
+end;
+
+procedure pascal_program;
+var
+    token: integer;
+begin
+    token := strip_whitespace();
+    if token <> _PROGRAM then
+    begin
+        error('Expecting PROGRAM statement, got: ', yytext)
+    end;
+end;
+
+begin
+    assign(yyinput, 'test.pas');
+    assign(yyoutput, '');
+    reset(yyinput); rewrite(yyoutput);
+    yylineno := 0;
+    yyclear;
+
+    pascal_program();
+end.