From 8ba5abf950a62e6c6745167af26097f328209f46 Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Tue, 24 May 2022 19:22:32 +0200 Subject: [PATCH 1/4] Move the body of the pallenec to the src/ directory We had left the pallenec out of the list of Lua files in the run-lint, which allowed it to accumulate a bunch of Lint errors. To stop this from happening again, I moved the bulk of the pallenec into a module inside the src/ directory, where the linter can see it. I considered just telling the linter to add the pallenec to the list but that was a bit awkward. Because the pallenec doesn't have a ".lua" extension, it would not be matched by the globs. --- bin/pallenec => src/pallene/pallenec.lua | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename bin/pallenec => src/pallene/pallenec.lua (100%) mode change 100755 => 100644 diff --git a/bin/pallenec b/src/pallene/pallenec.lua old mode 100755 new mode 100644 similarity index 100% rename from bin/pallenec rename to src/pallene/pallenec.lua From 7add14907f4fa5b56ee2d69f1445c9fb3086a977 Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Tue, 24 May 2022 19:27:46 +0200 Subject: [PATCH 2/4] Create the bin/pallenec stub This commit is separate from the previous one, to make the diff easier to read (move and modify the file in two separate commits). --- bin/pallenec | 3 +++ src/pallene/pallenec.lua | 18 +++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) create mode 100755 bin/pallenec diff --git a/bin/pallenec b/bin/pallenec new file mode 100755 index 00000000..0829503e --- /dev/null +++ b/bin/pallenec @@ -0,0 +1,3 @@ +#!/usr/bin/env lua +local pallenec = require "pallene.pallenec" +pallenec.main() diff --git a/src/pallene/pallenec.lua b/src/pallene/pallenec.lua index 55f2d0fc..6fcb2ce5 100644 --- a/src/pallene/pallenec.lua +++ b/src/pallene/pallenec.lua @@ -1,5 +1,3 @@ -#!/usr/bin/env lua - local argparse = require "argparse" local driver = require "pallene.driver" @@ -7,6 +5,8 @@ local print_ir = require "pallene.print_ir" local util = require "pallene.util" local C = require "pallene.C" +local pallenec = {} + -- -- Command-line options -- @@ -65,9 +65,13 @@ local function do_print_ir() io.stdout:write(print_ir(module)) end -if args.emit_c then compile("pln", "c") -elseif args.emit_lua then compile("pln", "lua") -elseif args.compile_c then compile("c" , "so") -elseif args.print_ir then do_print_ir() -else --[[default]] compile("pln", "so") +function pallenec.main() + if args.emit_c then compile("pln", "c") + elseif args.emit_lua then compile("pln", "lua") + elseif args.compile_c then compile("c" , "so") + elseif args.print_ir then do_print_ir() + else --[[default]] compile("pln", "so") + end end + +return pallenec From 3c5b72d010cd2f6187f8e75843443dccfdec9177 Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Tue, 24 May 2022 19:11:21 +0200 Subject: [PATCH 3/4] Fix lint errors in pallenec Unused variables, lack of copyright headers, etc. --- src/pallene/pallenec.lua | 74 +++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/src/pallene/pallenec.lua b/src/pallene/pallenec.lua index 6fcb2ce5..5670fdee 100644 --- a/src/pallene/pallenec.lua +++ b/src/pallene/pallenec.lua @@ -1,60 +1,58 @@ -local argparse = require "argparse" +-- Copyright (c) 2022, The Pallene Developers +-- Pallene is licensed under the MIT license. +-- Please refer to the LICENSE and AUTHORS files for details +-- SPDX-License-Identifier: MIT + +-- PALLENEC SCRIPT +-- =============== +-- This is the main entry point for the pallenec compiler +local argparse = require "argparse" local driver = require "pallene.driver" local print_ir = require "pallene.print_ir" local util = require "pallene.util" -local C = require "pallene.C" local pallenec = {} --- --- Command-line options --- - -local p = argparse("pallenec", "Pallene compiler") -p:argument("source_file", "File to compile") - --- What the compiler should output. -p:mutex( - p:flag("--emit-c", "Generate a .c file instead of an executable"), - p:flag("--emit-lua", "Generate a .lua file instead of an executable"), - p:flag("--compile-c", "Compile a .c file generated by --emit-c"), - p:flag("--print-ir", "Show the intermediate representation for a program") -) - --- Optimization levels for the Pallene and C compiler. (-O0 disables optimizations for both) --- NOTE: *For C compiler only* this option may be overridden using the CFLAGS environment variable. -p:option("-O", "Optimization level") - :args(1):convert(tonumber) - :choices({"0", "1", "2", "3"}) - :default(2) - -p:option("-o --output", "Output file path") - - -local args = p:parse() - -local opt_level = args.O - -- For compilation errors that don't happen inside a source file. -- Inspired by gcc, eg. "gcc: fatal error: no input files". local compiler_name = arg[0] +-- Command-line options +local args +do + local p = argparse("pallenec", "Pallene compiler") + p:argument("source_file", "File to compile") + + -- What the compiler should output. + p:mutex( + p:flag("--emit-c", "Generate a .c file instead of an executable"), + p:flag("--emit-lua", "Generate a .lua file instead of an executable"), + p:flag("--compile-c", "Compile a .c file generated by --emit-c"), + p:flag("--print-ir", "Show the intermediate representation for a program") + ) + + p:option("-O", "Optimization level") + :args(1):convert(tonumber) + :choices({"0", "1", "2", "3"}) + :default(2) + + p:option("-o --output", "Output file path") + + args = p:parse() +end + local function compile(in_ext, out_ext) - local ok, errs = driver.compile(compiler_name, opt_level, in_ext, out_ext, args.source_file, + local ok, errs = driver.compile(compiler_name, args.O, in_ext, out_ext, args.source_file, args.output) if not ok then util.abort(table.concat(errs, "\n")) end end - local function compile_up_to(stop_after) - local filename = args.source_file - local opt_level = args.O - - local input, err = driver.load_input(filename) + local input, err = driver.load_input(args.source_file) if err then util.abort(err) end - local out, errs = driver.compile_internal(filename, input, stop_after, opt_level) + local out, errs = driver.compile_internal(args.source_file, input, stop_after, args.O) if not out then util.abort(table.concat(errs, "\n")) end return out From 1c9c7753adf20ee0e37febb19bbcdb88ead1bc15 Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Tue, 24 May 2022 19:14:47 +0200 Subject: [PATCH 4/4] Rename args --> opts To avoid confusion with the similarly-named "arg" --- src/pallene/pallenec.lua | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/pallene/pallenec.lua b/src/pallene/pallenec.lua index 5670fdee..8821dc1b 100644 --- a/src/pallene/pallenec.lua +++ b/src/pallene/pallenec.lua @@ -19,7 +19,7 @@ local pallenec = {} local compiler_name = arg[0] -- Command-line options -local args +local opts do local p = argparse("pallenec", "Pallene compiler") p:argument("source_file", "File to compile") @@ -39,20 +39,20 @@ do p:option("-o --output", "Output file path") - args = p:parse() + opts = p:parse() end local function compile(in_ext, out_ext) - local ok, errs = driver.compile(compiler_name, args.O, in_ext, out_ext, args.source_file, - args.output) + local ok, errs = driver.compile(compiler_name, opts.O, in_ext, out_ext, opts.source_file, + opts.output) if not ok then util.abort(table.concat(errs, "\n")) end end local function compile_up_to(stop_after) - local input, err = driver.load_input(args.source_file) + local input, err = driver.load_input(opts.source_file) if err then util.abort(err) end - local out, errs = driver.compile_internal(args.source_file, input, stop_after, args.O) + local out, errs = driver.compile_internal(opts.source_file, input, stop_after, opts.O) if not out then util.abort(table.concat(errs, "\n")) end return out @@ -64,10 +64,10 @@ local function do_print_ir() end function pallenec.main() - if args.emit_c then compile("pln", "c") - elseif args.emit_lua then compile("pln", "lua") - elseif args.compile_c then compile("c" , "so") - elseif args.print_ir then do_print_ir() + if opts.emit_c then compile("pln", "c") + elseif opts.emit_lua then compile("pln", "lua") + elseif opts.compile_c then compile("c" , "so") + elseif opts.print_ir then do_print_ir() else --[[default]] compile("pln", "so") end end