From: Karim Hamidou Date: Thu, 31 Dec 2015 17:42:54 +0000 (+0100) Subject: tiny.pas can now parse a basic, empty pascal file. X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=89a57ca8837c68d5d7d8c235087dc813e1269ea0;p=tiny.pas.git tiny.pas can now parse a basic, empty pascal file. --- diff --git a/test.pas b/test.pas deleted file mode 100644 index d7cb8bd..0000000 --- a/test.pas +++ /dev/null @@ -1,15 +0,0 @@ -{ Test program } -program test; - -uses - SysUtils; - -var - c: char; -begin - c := 'a'; - if (c = 'b') or (c = 'c') then - begin - Writeln('Wot?'); - end; -end. diff --git a/tests/1_empty.pas b/tests/1_empty.pas new file mode 100644 index 0000000..87064bf --- /dev/null +++ b/tests/1_empty.pas @@ -0,0 +1,6 @@ +{ Test program } +program test; + +begin + +end. diff --git a/tiny.pas b/tiny.pas index c8cbee7..f3bcac1 100644 --- a/tiny.pas +++ b/tiny.pas @@ -30,6 +30,8 @@ const _DIV = 20; _FUNCTION = 21; _PROCEDURE = 22; + _EQUAL = 23; + _ASSIGNMENT = 24; { Lexing-related values. } var @@ -71,9 +73,7 @@ begin Writeln(yyoutput, ''); end; -{ Get a character from the file. Returns - yyunreadch if it's defined. -} +{ Get a character from the file. Returns yyunreadch if it's defined. } function Getch: Char; var c: Char; @@ -102,7 +102,6 @@ end; { As far as lexers go, this one is pretty slow because - it reads everything byte-by-byte. } function NextToken: integer; @@ -142,13 +141,21 @@ begin if c = ';' then exit(_SEMICOLON) else if c = '.' then - exit(_DOT); + exit(_DOT) + else if c = ':' then + begin + c := Getch(); + + if c <> '=' then + error('expected :=, got ', yytext); + exit(_ASSIGNMENT); + end; if ((c >= 'a') and (c <= 'z')) or ((c >= 'A') and (c <= 'Z')) then begin repeat yytext := yytext + c; c := Getch(); - until (c = ' ') or (c = '\n') or (c = '\t') or (c = ';') or (c = '.'); + until (c = ' ') or (c = #10) or (c = #09) or (c = ';') or (c = '.'); { If we're here, we've read one character too far. Put it back. } Ungetc(c); @@ -162,6 +169,10 @@ begin exit(_USES) else if CompareText(yytext, 'if') = 0 then exit(_IF) + else if CompareText(yytext, 'begin') = 0 then + exit(_BEGIN) + else if CompareText(yytext, 'end') = 0 then + exit(_END) else exit(_IDENTIFIER); end; @@ -172,12 +183,12 @@ var token: Integer; begin if yytoken = 0 then - exit(nextToken()); + exit(nextToken()) else begin token := yytoken; - yytoken := 0 - exit(token); + yytoken := 0; + exit(token) end; end; @@ -188,21 +199,33 @@ end; { I'm using the Pascal grammar from http://www2.informatik.uni-halle.de/lehre/pascal/sprache/pas_bnf.html } + +procedure pascal_program; Forward; +procedure code_block; Forward; +procedure statement_list; Forward; +procedure expression; Forward; + procedure pascal_program; var token: integer; begin token := yylex(); if token <> _PROGRAM then - error('expecting PROGRAM statement, got: ', yytext); + begin + error('expecting PROGRAM statement, got: ', yytext) + end; token := yylex(); if token <> _IDENTIFIER then - error('expecting identifier, got: ', yytext); + begin + error('expecting identifier, got: ', yytext) + end; token := yylex(); if token <> _SEMICOLON then - error('expecting semicolon, got: ', yytext); + begin + error('expecting semicolon, got: ', yytext) + end; code_block(); @@ -233,18 +256,31 @@ var begin token := yylex(); - if token := _IDENTIFIER then - expression(); - else - yyunread(token); - exit(); + if token = _IDENTIFIER then + begin + token := yylex(); + if token = _ASSIGNMENT then + expression() + else + error('expecting '':='', got: ', yytext); + end + else if token = _END then + begin + yyunread(token); + exit() + end end; procedure expression; var - token; integer; + token: integer; begin token := yylex(); + + if token <> _NUMBER then + error('expecting number, got: ', yytext); + + token := yylex(); end; begin @@ -253,5 +289,5 @@ begin yysetup(); pascal_program(); - Writeln('Program compiled.'); + Writeln('Program compiled.') end.