From f164898461d78f89f0edee80502750fe65977c4d Mon Sep 17 00:00:00 2001 From: Karim Hamidou Date: Tue, 29 Dec 2015 19:48:22 +0100 Subject: [PATCH] add a semi-functional lexer. --- Makefile | 11 +---- test.pas | 13 +++++ tiny.pas | 144 ++++++++++++++++++++++++++++++++++--------------------- 3 files changed, 105 insertions(+), 63 deletions(-) diff --git a/Makefile b/Makefile index 986438d..c41ecb0 100644 --- 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 diff --git a/test.pas b/test.pas index 8df2f75..d7cb8bd 100644 --- 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. diff --git a/tiny.pas b/tiny.pas index 630fc99..d1b0dc9 100644 --- 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. -- 2.43.0