Skip to content

Commit a1d34da

Browse files
committed
Code generator is mostly complete, major parts missing are function calls and strings,
but it successfully compiles and runs the 'artisanal-titan' examples, collected under 'examples/artisanal.titan'. Included patched Lua from 'artisanal-titan' to run the examples, but it is better if we tweak the build process to run with stock Lua.
1 parent 480b3ae commit a1d34da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+37371
-142
lines changed

examples/add.titan

-6
This file was deleted.

examples/artisanal.titan

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
-- Add two numbers
2+
3+
function foo(x: integer, y: integer): integer
4+
return x + y
5+
end
6+
7+
function filltable(N: integer): { integer }
8+
local xs: { integer } = {}
9+
for i: integer = 1,N do
10+
xs[i] = 10*i
11+
end
12+
return xs
13+
end
14+
15+
16+
function sieve(N: integer): {integer}
17+
18+
local is_prime: {boolean} = {}
19+
is_prime[1] = false
20+
for n: integer=2,N do
21+
is_prime[n] = true
22+
end
23+
24+
local nprimes: integer = 0
25+
local primes: {integer} = {}
26+
27+
for n: integer=1,N do
28+
if is_prime[n] then
29+
nprimes = nprimes + 1;
30+
primes[nprimes] = n
31+
for m:integer = n+n, N, n do
32+
is_prime[m] = false
33+
end
34+
end
35+
end
36+
37+
return primes
38+
end
39+
40+
41+
function sort(xs: {integer}): nil
42+
local N: integer = #xs
43+
for i: integer=1,N do
44+
45+
-- Find minimum
46+
local min_i: integer = i
47+
local min_x: integer = xs[i]
48+
for j: integer = i+1, N do
49+
local y: integer = xs[j]
50+
if y < min_x then
51+
min_i = j
52+
min_x = y
53+
end
54+
end
55+
56+
-- Move it to the front
57+
local tmp: integer = xs[i]
58+
xs[i] = xs[min_i]
59+
xs[min_i] = tmp
60+
end
61+
end
62+
63+
function sum(N: integer): integer
64+
local res = 0
65+
for i: integer=1,N do
66+
res = res + i
67+
end
68+
return res
69+
end

examples/test_fill_table.lua

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
local mod = require "artisanal"
2+
3+
local f = mod.filltable
4+
5+
local N = arg[1] and tonumber(arg[1]) or 1000
6+
7+
print("N="..N)
8+
local r = f(N)
9+
print(type(r))

examples/test_selection_sort.lua

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
local N = arg[1] and tonumber(arg[1]) or 5000
2+
3+
local sort = require("artisanal").sort
4+
5+
local function make_input(N)
6+
local xs = {}
7+
for i=1,N do
8+
xs[i] = N - i + 1
9+
end
10+
return xs
11+
end
12+
13+
local function print_array(xs)
14+
print( "{" .. table.concat(xs, ", ") .. "}" )
15+
end
16+
17+
local function is_sorted(xs)
18+
for i=2,#xs do
19+
if xs[i-1] > xs[i] then
20+
return false
21+
end
22+
end
23+
return true
24+
end
25+
26+
local xs = make_input(N)
27+
print('before', is_sorted(xs))
28+
sort(xs)
29+
print('after', is_sorted(xs))

examples/test_sieve.lua

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
local N = arg[1] and tonumber(arg[1]) or 100
2+
3+
local f = require("artisanal").sieve
4+
5+
local ps = f(N)
6+
print(#ps)

examples/test_sum_1_N.lua

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
local N = arg[1] and tonumber(arg[1]) or 500000000
2+
3+
local f = require("artisanal").sum
4+
5+
print("N="..N)
6+
local r = f(N)
7+
print(r)
8+

lua/Makefile

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Makefile for installing Lua
2+
# See doc/readme.html for installation and customization instructions.
3+
4+
# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================
5+
6+
# Your platform. See PLATS for possible values.
7+
PLAT= none
8+
9+
# Where to install. The installation starts in the src and doc directories,
10+
# so take care if INSTALL_TOP is not an absolute path. See the local target.
11+
# You may want to make INSTALL_LMOD and INSTALL_CMOD consistent with
12+
# LUA_ROOT, LUA_LDIR, and LUA_CDIR in luaconf.h.
13+
INSTALL_TOP= /usr/local
14+
INSTALL_BIN= $(INSTALL_TOP)/bin
15+
INSTALL_INC= $(INSTALL_TOP)/include
16+
INSTALL_LIB= $(INSTALL_TOP)/lib
17+
INSTALL_MAN= $(INSTALL_TOP)/man/man1
18+
INSTALL_LMOD= $(INSTALL_TOP)/share/lua/$V
19+
INSTALL_CMOD= $(INSTALL_TOP)/lib/lua/$V
20+
21+
# How to install. If your install program does not support "-p", then
22+
# you may have to run ranlib on the installed liblua.a.
23+
INSTALL= install -p
24+
INSTALL_EXEC= $(INSTALL) -m 0755
25+
INSTALL_DATA= $(INSTALL) -m 0644
26+
#
27+
# If you don't have "install" you can use "cp" instead.
28+
# INSTALL= cp -p
29+
# INSTALL_EXEC= $(INSTALL)
30+
# INSTALL_DATA= $(INSTALL)
31+
32+
# Other utilities.
33+
MKDIR= mkdir -p
34+
RM= rm -f
35+
36+
# == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE =======
37+
38+
# Convenience platforms targets.
39+
PLATS= aix bsd c89 freebsd generic linux macosx mingw posix solaris
40+
41+
# What to install.
42+
TO_BIN= lua luac
43+
TO_INC= lua.h luaconf.h lualib.h lauxlib.h lua.hpp
44+
TO_LIB= liblua.a
45+
TO_MAN= lua.1 luac.1
46+
47+
# Lua version and release.
48+
V= 5.3
49+
R= $V.4
50+
51+
# Targets start here.
52+
all: $(PLAT)
53+
54+
$(PLATS) clean:
55+
cd src && $(MAKE) $@
56+
57+
test: dummy
58+
src/lua -v
59+
60+
install: dummy
61+
cd src && $(MKDIR) $(INSTALL_BIN) $(INSTALL_INC) $(INSTALL_LIB) $(INSTALL_MAN) $(INSTALL_LMOD) $(INSTALL_CMOD)
62+
cd src && $(INSTALL_EXEC) $(TO_BIN) $(INSTALL_BIN)
63+
cd src && $(INSTALL_DATA) $(TO_INC) $(INSTALL_INC)
64+
cd src && $(INSTALL_DATA) $(TO_LIB) $(INSTALL_LIB)
65+
cd doc && $(INSTALL_DATA) $(TO_MAN) $(INSTALL_MAN)
66+
67+
uninstall:
68+
cd src && cd $(INSTALL_BIN) && $(RM) $(TO_BIN)
69+
cd src && cd $(INSTALL_INC) && $(RM) $(TO_INC)
70+
cd src && cd $(INSTALL_LIB) && $(RM) $(TO_LIB)
71+
cd doc && cd $(INSTALL_MAN) && $(RM) $(TO_MAN)
72+
73+
local:
74+
$(MAKE) install INSTALL_TOP=../install
75+
76+
none:
77+
@echo "Please do 'make PLATFORM' where PLATFORM is one of these:"
78+
@echo " $(PLATS)"
79+
@echo "See doc/readme.html for complete instructions."
80+
81+
# make may get confused with test/ and install/
82+
dummy:
83+
84+
# echo config parameters
85+
echo:
86+
@cd src && $(MAKE) -s echo
87+
@echo "PLAT= $(PLAT)"
88+
@echo "V= $V"
89+
@echo "R= $R"
90+
@echo "TO_BIN= $(TO_BIN)"
91+
@echo "TO_INC= $(TO_INC)"
92+
@echo "TO_LIB= $(TO_LIB)"
93+
@echo "TO_MAN= $(TO_MAN)"
94+
@echo "INSTALL_TOP= $(INSTALL_TOP)"
95+
@echo "INSTALL_BIN= $(INSTALL_BIN)"
96+
@echo "INSTALL_INC= $(INSTALL_INC)"
97+
@echo "INSTALL_LIB= $(INSTALL_LIB)"
98+
@echo "INSTALL_MAN= $(INSTALL_MAN)"
99+
@echo "INSTALL_LMOD= $(INSTALL_LMOD)"
100+
@echo "INSTALL_CMOD= $(INSTALL_CMOD)"
101+
@echo "INSTALL_EXEC= $(INSTALL_EXEC)"
102+
@echo "INSTALL_DATA= $(INSTALL_DATA)"
103+
104+
# echo pkg-config data
105+
pc:
106+
@echo "version=$R"
107+
@echo "prefix=$(INSTALL_TOP)"
108+
@echo "libdir=$(INSTALL_LIB)"
109+
@echo "includedir=$(INSTALL_INC)"
110+
111+
# list targets that do not create files (but not all makes understand .PHONY)
112+
.PHONY: all $(PLATS) clean test install local none dummy echo pecho lecho
113+
114+
# (end of Makefile)

lua/README

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
This is Lua 5.3.4, released on 12 Jan 2017.
3+
4+
For installation instructions, license details, and
5+
further information about Lua, see doc/readme.html.
6+

0 commit comments

Comments
 (0)