From e62ec5ac36188cb12411a8c720daebce77ecf645 Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Mon, 27 May 2024 00:11:26 -0300 Subject: Set up a "build system" --- src/bin/cratera.lua | 21 ++++++++++++++++ src/bin/printtokens.lua | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/bin/cratera.lua create mode 100644 src/bin/printtokens.lua (limited to 'src/bin') diff --git a/src/bin/cratera.lua b/src/bin/cratera.lua new file mode 100644 index 0000000..fa94e96 --- /dev/null +++ b/src/bin/cratera.lua @@ -0,0 +1,21 @@ +--[[ + Cratera Interpreter + Copyright (C) 2024 Soni L. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +--]] + +local cratera = require "cratera" + +error("not implemented") diff --git a/src/bin/printtokens.lua b/src/bin/printtokens.lua new file mode 100644 index 0000000..49827da --- /dev/null +++ b/src/bin/printtokens.lua @@ -0,0 +1,67 @@ +--[[ + This file is part of Cratera Compiler + Copyright (C) 2019 Soni L. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +--]] + +-- this isn't supposed to be installed or anything it's just a basic CLI to +-- print a lua or cratera file as a token stream. + +local parser = require "cratera.parser" + +-- CLI argument rules +local defs = parser.selfify({}) +defs['-'] = function(state, token) + if state.filename then + error("Must specify only one filename") + end + state.filename = true + state.file = io.stdin + return "self" +end +defs[parser.FALLBACK] = function(state, token) + if state.filename then + error("Must specify only one filename") + end + state.filename = token + state.file, state.err = io.open(state.filename, "r") + return "self" +end +defs[parser.EOZ] = function(state, token) + if not state.file then + error((state.filename and (state.err or "") or "No file specified") ) + end + return {} +end +defs[-1] = function(state, token, rule) + if token ~= parser.EOZ and token:sub(1,1) == "-" and not rule then + error("Unknown option: " .. token) + end +end +defs['--'] = parser.selfify({[parser.FALLBACK] = defs[parser.FALLBACK], [parser.EOZ] = defs[parser.EOZ]}) + +local state = parser.parse(defs, arg) +local luatokens = require "cratera.luatokens" +local file = state.file +local tokens = luatokens.defs +local state, err, etoken, estate = parser.parse(tokens, function() return file:read(8192) end) +if state then + for i,v in ipairs(state) do + v = luatokens.reverse_keywords[v] or luatokens.reverse_tokens[v] or v + print(i, v) -- TODO formatting + end +else + print("Parse error") +end -- cgit 1.4.1