Adding fancy makefile that compiles all the moonscript files and

precompiles all the nomsu files and can build a nice executable file and
install it wherever you like.
This commit is contained in:
Bruce Hill 2018-06-22 02:40:11 -07:00
parent 86a3219e7f
commit 126678f737
7 changed files with 78 additions and 6 deletions

65
Makefile Normal file
View File

@ -0,0 +1,65 @@
# Nomsu makefile
# To build, run `make`
# To install,
# ========= User-controlled variables ========
LUA= lua
LUA_BIN= /usr/local/bin/$(LUA)
PREFIX=/usr/local
BIN_DIR= $(PREFIX)/bin
NOMSU_DIR= $(PREFIX)/lib/nomsu
# ========= You shouldn't need to mess with any of these variables below ================
MOON_FILES= code_obj.moon error_handling.moon nomsu.moon nomsu_compiler.moon nomsu_tree.moon parser.moon
LUA_FILES= code_obj.lua consolecolors.lua error_handling.lua nomsu.lua nomsu_compiler.lua \
nomsu_tree.lua parser.lua utils.lua uuid.lua
CORE_NOM_FILES= $(wildcard core/*.nom)
CORE_LUA_FILES= $(patsubst %.nom,%.lua,$(CORE_NOM_FILES))
LIB_NOM_FILES= $(wildcard lib/*.nom)
LIB_LUA_FILES= $(patsubst %.nom,%.lua,$(LIB_NOM_FILES))
PEG_FILE= nomsu.peg
NOMSU_HEADER=\#!$(LUA_BIN)\npackage.path = [[$(realpath $(NOMSU_DIR))/?.lua;]]..package.path\npackage.nomsupath = [[$(realpath $(NOMSU_DIR))]]
all: build optimize
.PHONY: test
test: build optimize
./nomsu tests
%.lua: %.moon
@moonc $<
%.lua: %.nom
@./nomsu -c $<
.PHONY: check_header
check_header:
@if [ "`head -n 3 nomsu 2>/dev/null`" != "`echo '$(NOMSU_HEADER)'`" ]; then rm -f nomsu core/*.lua lib/*.lua; fi
nomsu: nomsu.lua
@echo '$(NOMSU_HEADER)' | cat - nomsu.lua > nomsu
@chmod +x nomsu
@echo "Built nomsu binary"
build: $(LUA_FILES) check_header nomsu
.PHONY: optimize
optimize: build $(CORE_LUA_FILES) $(LIB_LUA_FILES)
.PHONY: clean
clean:
rm -rf nomsu core/*.lua lib/*.lua $(BIN_DIR)/nomsu $(NOMSU_DIR)
.PHONY: install
install: all
mkdir -p $(BIN_DIR) && cp nomsu $(BIN_DIR)
mkdir -p $(NOMSU_DIR) && cp -r $(LUA_FILES) $(PEG_FILE) core lib $(NOMSU_DIR)
.PHONY: uninstall
uninstall: all
rm -rf $(NOMSU_DIR) $(BIN_DIR)/nomsu
# eof

View File

@ -169,7 +169,6 @@ run = function()
else else
print_file = io.stdout print_file = io.stdout
end end
nomsu.skip_precompiled = not args.optimized
if print_file == nil then if print_file == nil then
nomsu.print = function() end nomsu.print = function() end
elseif print_file ~= io.stdout then elseif print_file ~= io.stdout then
@ -195,6 +194,7 @@ run = function()
to_run[f] = true to_run[f] = true
end end
end end
nomsu.skip_precompiled = to_run
if args.compile or args.verbose then if args.compile or args.verbose then
nomsu.on_compile = function(code, from_file) nomsu.on_compile = function(code, from_file)
if not (to_run[from_file]) then if not (to_run[from_file]) then

View File

@ -135,7 +135,6 @@ run = ->
elseif args.print_file then io.open(args.print_file, 'w') elseif args.print_file then io.open(args.print_file, 'w')
else io.stdout else io.stdout
nomsu.skip_precompiled = not args.optimized
if print_file == nil if print_file == nil
nomsu.print = -> nomsu.print = ->
elseif print_file != io.stdout elseif print_file != io.stdout
@ -155,6 +154,8 @@ run = ->
input_files[#input_files+1] = f input_files[#input_files+1] = f
to_run[f] = true to_run[f] = true
nomsu.skip_precompiled = to_run
if args.compile or args.verbose if args.compile or args.verbose
nomsu.on_compile = (code, from_file)-> nomsu.on_compile = (code, from_file)->
return unless to_run[from_file] return unless to_run[from_file]

View File

@ -465,7 +465,7 @@ do
ret = self:run_lua(file, Source(filename, 1, #file)) ret = self:run_lua(file, Source(filename, 1, #file))
elseif match(filename, "%.nom$") or match(filename, "^/dev/fd/[012]$") then elseif match(filename, "%.nom$") or match(filename, "^/dev/fd/[012]$") then
local ran_lua local ran_lua
if not self.skip_precompiled then if not self.skip_precompiled or not self.skip_precompiled[filename] then
local lua_filename = gsub(filename, "%.nom$", ".lua") local lua_filename = gsub(filename, "%.nom$", ".lua")
do do
local file = FILE_CACHE[lua_filename] local file = FILE_CACHE[lua_filename]

View File

@ -319,7 +319,7 @@ with NomsuCompiler
file = assert(FILE_CACHE[filename], "Could not find file: #{filename}") file = assert(FILE_CACHE[filename], "Could not find file: #{filename}")
ret = @run_lua file, Source(filename, 1, #file) ret = @run_lua file, Source(filename, 1, #file)
elseif match(filename, "%.nom$") or match(filename, "^/dev/fd/[012]$") elseif match(filename, "%.nom$") or match(filename, "^/dev/fd/[012]$")
ran_lua = if not @skip_precompiled -- Look for precompiled version ran_lua = if not @skip_precompiled or not @skip_precompiled[filename] -- Look for precompiled version
lua_filename = gsub(filename, "%.nom$", ".lua") lua_filename = gsub(filename, "%.nom$", ".lua")
if file = FILE_CACHE[lua_filename] if file = FILE_CACHE[lua_filename]
ret = @run_lua file, Source(lua_filename, 1, #file) ret = @run_lua file, Source(lua_filename, 1, #file)

View File

@ -127,7 +127,10 @@ do
ident <- [a-zA-Z_][a-zA-Z0-9_]* ident <- [a-zA-Z_][a-zA-Z0-9_]*
comment <- "--" [^%nl]* comment <- "--" [^%nl]*
]]) ]])
local nomsu_peg = peg_tidier:match(io.open((package.nomsupath or '.') .. "/nomsu.peg"):read('*a')) local peg_file = io.open("nomsu.peg") or (package.nomsupath and io.open(package.nomsupath .. "/nomsu.peg"))
assert(peg_file, "could not find nomsu.peg file")
local nomsu_peg = peg_tidier:match(peg_file:read('*a'))
peg_file:close()
NOMSU_PATTERN = re.compile(nomsu_peg, NOMSU_DEFS) NOMSU_PATTERN = re.compile(nomsu_peg, NOMSU_DEFS)
end end
local parse local parse

View File

@ -98,7 +98,10 @@ NOMSU_PATTERN = do
ident <- [a-zA-Z_][a-zA-Z0-9_]* ident <- [a-zA-Z_][a-zA-Z0-9_]*
comment <- "--" [^%nl]* comment <- "--" [^%nl]*
]] ]]
nomsu_peg = peg_tidier\match(io.open((package.nomsupath or '.').."/nomsu.peg")\read('*a')) peg_file = io.open("nomsu.peg") or (package.nomsupath and io.open(package.nomsupath.."/nomsu.peg"))
assert(peg_file, "could not find nomsu.peg file")
nomsu_peg = peg_tidier\match(peg_file\read('*a'))
peg_file\close!
re.compile(nomsu_peg, NOMSU_DEFS) re.compile(nomsu_peg, NOMSU_DEFS)
parse = (nomsu_code, source=nil)-> parse = (nomsu_code, source=nil)->