From 36c1dea04e87d726a28c8edb321d8783836ae672 Mon Sep 17 00:00:00 2001 From: David Schmenk Date: Tue, 2 Jan 2018 20:17:55 -0800 Subject: [PATCH] Get PLASM parsing --- src/libsrc/fileio.pla | 2 +- src/toolsrc/codegen.pla | 6 ++-- src/toolsrc/lex.pla | 35 ++++++++++++--------- src/toolsrc/parse.pla | 29 +++++++---------- src/toolsrc/plasm.pla | 70 ++++++++++++++++++++--------------------- 5 files changed, 70 insertions(+), 72 deletions(-) diff --git a/src/libsrc/fileio.pla b/src/libsrc/fileio.pla index f6bdfcc..cb0fd72 100644 --- a/src/libsrc/fileio.pla +++ b/src/libsrc/fileio.pla @@ -44,7 +44,7 @@ predef a23newline(refnum, emask, nlchar), a2readblock(unit, buf, block), a2write // // Exported function table. // -export word fileio[] = @a2getpfx, @a23setpfx, @a2getfileinfo, @a23geteof, @a2open, @a23close +export word fileio[] = @a2getpfx, @a23setpfx, @a2getfileinfo, @a23geteof, @a2openbuf, @a2open, @a23close word = @a23read, @a2write, @a2create, @a23destroy word = @a23newline, @a2readblock, @a2writeblock // diff --git a/src/toolsrc/codegen.pla b/src/toolsrc/codegen.pla index 610a0d4..eeeb5cc 100644 --- a/src/toolsrc/codegen.pla +++ b/src/toolsrc/codegen.pla @@ -235,7 +235,7 @@ def id_lookup(nameptr, len) idptr = idmatch(nameptr, len, idlocal_tbl, locals) if not idptr idptr = idmatch(nameptr, len, idglobal_tbl, globals) - if not idptr; exit_err(ERR_UNDECL|ERR_ID); fin +// if not idptr; exit_err(ERR_UNDECL|ERR_ID); fin fin return idptr end @@ -243,7 +243,7 @@ def idglobal_lookup(nameptr, len) word idptr idptr = idmatch(nameptr, len, idglobal_tbl, globals) - if not idptr; exit_err(ERR_UNDECL|ERR_ID); fin +// if not idptr; exit_err(ERR_UNDECL|ERR_ID); fin return idptr end def emit_iddata(value, size, namestr)#0 @@ -290,7 +290,7 @@ def idfunc_set(namestr, len, tag, cparms, cvals)#0 idptr = idglobal_lookup(namestr, len) if idptr - idptr=>idtype = FUNC_TYPE + if not idptr=>idtype & FUNC_TYPE; exit_err(ERR_UNDECL|ERR_CODE); fin idptr=>idval = tag idptr->funcparms = cparms idptr->funcvals = cvals diff --git a/src/toolsrc/lex.pla b/src/toolsrc/lex.pla index 0f78d10..9f5a78b 100644 --- a/src/toolsrc/lex.pla +++ b/src/toolsrc/lex.pla @@ -17,10 +17,10 @@ end def isalphanum(c) if c >= 'A' and c <= 'Z' return TRUE - elsif c >= '0' and c <= '9' - return TRUE elsif c >= 'a' and c <= 'z' return TRUE + elsif c >= '0' and c <= '9' + return TRUE elsif c == '_' return TRUE fin @@ -148,14 +148,14 @@ def scan is 't' ^constval = $09; break otherwise - constval = ^scanptr + ^constval = ^scanptr wend fin constval++ strconst++ scanptr++ loop - if !^scanptr; exit_err(ERR_INVAL|ERR_CONST); fin + if not ^scanptr; exit_err(ERR_INVAL|ERR_CONST); fin constval = @strconst scanptr++ break @@ -288,26 +288,31 @@ def nextln scanptr++ scan else - if token <> EOL_TKN or token <> EOF_TKN; exit_err("Extraneous characters"); fin + if token <> EOL_TKN and token <> EOF_TKN; puti(token&$7F); puts("Extraneous characters\n"); exit_err(0); fin scanptr = inbuff - ^instr = fileio:read(refnum, inbuff, 127) - if instr - ^(inbuff + instr + 1) = 0 // NULL terminate string + ^instr = fileio:read(refnum, inbuff, 127) + if ^instr + ^(instr + ^instr) = NULL // NULL terminate string lineno++ if !(lineno & $0F); putc('.'); fin if scan == INCLUDE_TKN - if incref; exit_err("Nested INCLUDEs not allowed"); fin - if scan <> STR_TKN; exit_err("Missing INCLUDE file"); fin - incfile = scanptr - constval - memcpy(@incfile + 1, constval, incfile) + if incref; puts("Nested INCLUDEs not allowed\n"); exit_err(0); fin + if scan <> STR_TKN; puts("Missing INCLUDE file\n"); exit_err(0); fin + strcpy(@incfile, constval) sysincbuf = heapallocalign(1024, 8, @sysincfre) - incref = fileio:openbuf(@incfile, sysincbuf) - if not incref; exit_err("Unable to open INCLUDE file"); fin + incref = fileio:openbuf(@incfile, sysincbuf) + if not incref + puts("Unable to open INCLUDE file: ") + puts(@incfile) + putln + exit_err(0) + fin fileio:newline(incref, $7F, $0D) refnum = incref parsefile = @incfile srcline = lineno lineno = 0 + scan return nextln fin else @@ -316,7 +321,7 @@ def nextln heaprelease(sysincfre) incref = 0 refnum = srcref - parsefile = srcfile + parsefile = @srcfile lineno = srcline return nextln else diff --git a/src/toolsrc/parse.pla b/src/toolsrc/parse.pla index 19f6758..2c78a88 100644 --- a/src/toolsrc/parse.pla +++ b/src/toolsrc/parse.pla @@ -13,16 +13,10 @@ def pop_op return opstack[opsp] end def tos_op - if opsp < 0 - return 0 - fin - return opstack[opsp] + return opsp < 0 ?? 0 :: opstack[opsp-1] end def tos_op_prec(tos) - if opsp < tos - return 100 - fin - return precstack[opsp] + return opsp <= tos ?? 100 :: precstack[opsp-1] end def push_val(value, size, type)#0 if valsp == 16; exit_err(ERR_OVER|ERR_CODE|ERR_FRAME); fin @@ -277,14 +271,14 @@ def parse_value(codeseq, rvalue)#2 when token is ID_TKN idptr = id_lookup(tknptr, tknlen) - if !idptr; return NULL, 0; fin - if !(idptr=>idtype); return NULL, 0; fin + if not idptr; return NULL, 0; fin + if not idptr=>idtype; return NULL, 0; fin type = type | idptr=>idtype value = idptr=>idval if type & CONST_TYPE valseq = gen_const(NULL, value) else - valseq = type & LOCAL_TYPE ?? gen_oplcl(NULL, LADDR_CODE, value) :: gen_opglbl(NULL, GADDR_CODE, value, type) + valseq = type & LOCAL_TYPE ?? gen_oplcl(NULL, LADDR_CODE, value) :: gen_opglbl(NULL, GADDR_CODE, value, 0) fin if type & FUNC_TYPE cfnparms = idptr->funcparms @@ -858,6 +852,7 @@ def parse_stmnt is END_TKN is DONE_TKN is DEF_TKN + is EOF_TKN return FALSE otherwise rewind(tknptr) @@ -1165,14 +1160,10 @@ def parse_defs fin emit_ctag(func_tag) retfunc_tag = ctag_new - while parse_vars(LOCAL_TYPE) - nextln - loop + while parse_vars(LOCAL_TYPE); nextln; loop emit_enter(cfnparms) prevstmnt = 0 - while parse_stmnt - nextln - loop + while parse_stmnt; nextln; loop infunc = FALSE if token <> END_TKN; exit_err(ERR_MISS|ERR_CLOSE|ERR_STATE); fin scan @@ -1198,8 +1189,9 @@ def parse_module#0 // // Compile module // + while parse_mods; nextln; loop while parse_vars(GLOBAL_TYPE); nextln; loop - while parse_defs; nextln; loop + while parse_defs; nextln; loop entrypoint = codeptr prevstmnt = 0 idlocal_init @@ -1211,6 +1203,7 @@ def parse_module#0 emit_const(0) emit_leave fin + if token <> DONE_TKN; parse_warn("Missing DONE\n"); fin //dumpsym(idglobal_tbl, globals) fin end diff --git a/src/toolsrc/plasm.pla b/src/toolsrc/plasm.pla index b45da97..aae0835 100644 --- a/src/toolsrc/plasm.pla +++ b/src/toolsrc/plasm.pla @@ -196,11 +196,11 @@ byte = 10 // Lowest precedence byte[16] opstack byte[16] precstack -word opsp = 0 +word opsp word[16] valstack byte[16] sizestack byte[16] typestack -word valsp = 0 +word valsp // // Constant code group // @@ -333,22 +333,22 @@ byte outflags // ProDOS/SOS file references // byte refnum, srcref, incref -byte[32] srcfile -byte[32] incfile -byte[32] relfile +byte[32] srcfile, incfile, relfile word parsefile // Pointer to current file word sysincbuf, sysincfre // System I/O buffer for include files word srcline // Saved source line number // // Scanner variables // -const inbuff = $0200 -const instr = $01FF -word scanptr = inbuff -byte scanchr, token, tknlen +//const inbuff = $0200 +//const instr = $01FF +//word scanptr = inbuff +byte token = EOL_TKN +word instr, inbuff, scanptr +byte scanchr, tknlen word tknptr, parserrln word constval -word lineno = 0 +word lineno // // Parser variables // @@ -369,9 +369,9 @@ word[LAMBDANUM] lambda_seq word[LAMBDANUM] lambda_tag predef parse_constexpr#3, parse_expr(codeseq)#2, parse_lambda // -// Arg pointer - overlay setjmp pointer +// Arg pointer // -word[] arg +word arg, opt // // Long jump environment // @@ -418,13 +418,9 @@ def nametostr(namestr, len, strptr)#0 memcpy(strptr + 1, namestr, len) end def putcurln#0 - puts(parsefile); putc('['); puti(lineno); puts("] ") -end -def putmrkr#0 byte i - - putln - puts(instr) + putln; puts(parsefile); putc('['); puti(lineno); puts("]\n") + puts(instr); putln for i = tknptr - inbuff downto 1 putc(' ') next @@ -436,8 +432,7 @@ end def exit_err(err)#0 byte i - putcurln - puts("Error:") + puts("\nError:") if err & ERR_DUP; puts("duplicate "); fin if err & ERR_UNDECL; puts("undeclared "); fin if err & ERR_INVAL; puts("invalid "); fin @@ -447,14 +442,14 @@ def exit_err(err)#0 if err & ERR_LOCAL; puts("local "); fin if err & ERR_GLOBAL; puts("global "); fin if err & ERR_CODE; puts("code "); fin - if err & ERR_ID; puts("identifier "); fin + if err & ERR_ID; puts("identifier "); fin if err & ERR_CONST; puts("constant"); fin if err & ERR_INIT; puts("initializer"); fin if err & ERR_STATE; puts("statement"); fin - if err * ERR_FRAME; puts("frame"); fin + if err & ERR_FRAME; puts("frame"); fin if err & ERR_TABLE; puts("table"); fin if err & ERR_SYNTAX; puts("syntax"); fin - putmrkr + putcurln fileio:close(0) // Close all open files longjmp(exit, TRUE) end @@ -463,10 +458,9 @@ end // def parse_warn(msg)#0 if outflags & WARNINGS - putcurln - puts("Warning:") + puts("\nWarning:") puts(msg) - putmrkr + putcurln fin end // @@ -480,19 +474,21 @@ include "toolsrc/parse.pla" // arg = argNext(argFirst) if ^arg and ^(arg + 1) == '-' - arg = arg + 2 + opt = arg + 2 while TRUE - if toupper(^arg) == 'O' + if toupper(^opt) == 'O' outflags = outflags | OPTIMIZE - arg++ - if ^arg == '2' + opt++ + if ^opt == '2' outflags = outflags | OPTIMIZE2 - arg++ + opt++ fin - elsif toupper(^arg) == 'N' + elsif toupper(^opt) == 'N' outflags = outflags | NO_COMBINE - elsif toupper(^arg) == 'W' + opt++ + elsif toupper(^opt) == 'W' outflags = outflags | WARNINGS + opt++ else break fin @@ -512,7 +508,11 @@ if srcfile and relfile fileio:newline(srcref, $7F, $0D) refnum = srcref parsefile = @srcfile - exit = heapalloc(t_longjmp) + instr = heapalloc(128) + *instr = 0 + inbuff = instr + 1 + scanptr = inbuff + exit = heapalloc(t_longjmp) if not setjmp(exit) // // Parse source code module @@ -537,6 +537,6 @@ if srcfile and relfile puts("Error opening: "); puts(@srcfile); putln fin else - puts("Usage: +PLASM [-[W][O[2]][N]] \n") + puts("Usage: +PLASM [-[W][O[2]][N]] \n") fin done