From 5531888c8b72cb37bac3df48602a9770c22d0dbc Mon Sep 17 00:00:00 2001 From: helixhorned Date: Sun, 10 Jun 2012 18:56:18 +0000 Subject: [PATCH] Lunatic: take a stab at semantic actions; tweaks git-svn-id: https://svn.eduke32.com/eduke32@2748 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/lunatic/lunacon.lua | 58 ++++++++++++++++------ 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/polymer/eduke32/source/lunatic/lunacon.lua b/polymer/eduke32/source/lunatic/lunacon.lua index 2a40d06ea..da459923b 100644 --- a/polymer/eduke32/source/lunatic/lunacon.lua +++ b/polymer/eduke32/source/lunatic/lunacon.lua @@ -26,6 +26,30 @@ local function match_until(matchsp, untilsp) -- (!untilsp matchsp)* in PEG return (matchsp - Pat(untilsp))^0 end +local function printf(fmt, ...) + print(string.format(fmt, ...)) +end + + +---=== semantic action functions ===--- + +local function getlinecol(pos) end -- fwd-decl + +local function parse_number(numstr, pos) + local num = tonumber(numstr) + + -- TODO: print line number + if (num < -0x80000000 or num > 0xffffffff) then + printf("warning: number %s out of the range of a 32-bit integer", numstr) + num = 0/0 + elseif (num >= 0x80000000) then + printf("warning: number %s converted to a negative one", numstr) + num = num-0x100000000 + end + + return num +end + ----==== patterns ====---- @@ -47,7 +71,10 @@ local alphanum = alpha + Range("09") --local alnumtok = alphanum + Set("{}/\\*-_.") -- see isaltok() in gamedef.c --- basic lexical elements ("tokens") -local t_number = (Pat("0x") + "0X")*Range("09", "af", "AF")^1 + Range("09")^1 +local t_maybe_minus = (Pat("-") * sp0)^-1; +local t_number = lpeg.C( + t_maybe_minus * ((Pat("0x") + "0X")*Range("09", "af", "AF")^1 + Range("09")^1) + ) / parse_number -- Valid identifier names are disjunct from keywords! -- XXX: CON is more permissive with identifier name characters: local t_identifier = Var("t_identifier") @@ -601,20 +628,16 @@ local string = require("string") -- newlineidxs will contain the 1-based file offsets to "\n" characters local newlineidxs = {} -local function printf(fmt, ...) - print(string.format(fmt, ...)) -end - -- Returns index into the sorted table tab such that -- tab[index] <= searchelt < tab[index+1]. -- Preconditions: --- tab[i] < tab[i+1] for 1 <= i < #tab --- tab[1] <= searchelt < tab[#tab] +-- tab[i] < tab[i+1] for 0 <= i < #tab +-- tab[0] <= searchelt < tab[#tab] -- If #tab is less than 2, returns 0. This plays nicely with newline index -- tables like { [0]=0, [1]=len+1 }, e.g. if the file doesn't contain any. local function bsearch(tab, searchelt) -- printf("bsearch(tab, %d)", searchelt) - local l, r = 1, #tab + local l, r = 0, #tab local i if (r < 2) then @@ -642,7 +665,7 @@ local function bsearch(tab, searchelt) return l end -local function getlinecol(pos) +function getlinecol(pos) -- local local line = bsearch(newlineidxs, pos) assert(line and newlineidxs[line]<=pos and pos