Skip to content

Commit

Permalink
Merge pull request #631 from pallene-lang/optional-arguments
Browse files Browse the repository at this point in the history
Optional function arguments
  • Loading branch information
hugomg authored Sep 6, 2024
2 parents d14cdbb + 7d02b4e commit 104733f
Show file tree
Hide file tree
Showing 24 changed files with 261 additions and 144 deletions.
2 changes: 1 addition & 1 deletion benchmarks/binarytrees/lua.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
math.ln = math.log; local m = {}
local m = {}

function m.BottomUpTree(depth)
if depth > 0 then
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/binsearch/lua.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
math.ln = math.log; local m = {}
local m = {}

function m.binsearch(t, x)
-- lo <= x <= hi
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/centroid/lua.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
math.ln = math.log; local m = {}
local m = {}

function m.new(x, y)
return { x, y }
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/conway/lua.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
math.ln = math.log; local m = {}
local m = {}

local ALIVE = "*"
local DEAD = " "
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/fannkuchredux/lua.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
math.ln = math.log; local m = {}
local m = {}

function m.fannkuch(N)

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/fasta/lua.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
math.ln = math.log; local m = {}
local m = {}

local IM = 139968
local IA = 3877
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/mandelbrot/lua.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
math.ln = math.log; local m = {}
local m = {}

function m.mandelbrot(N)
local bits = 0
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/matmul/lua.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
math.ln = math.log; local m = {}
local m = {}

function m.matmul(A, B)
local C = {}
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/nbody/lua.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
math.ln = math.log; local m = {}
local m = {}



Expand Down
2 changes: 1 addition & 1 deletion benchmarks/objmandelbrot/lua.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
math.ln = math.log; local m = {}
local m = {}

function m.new(x, y)
return { x, y }
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/queen/lua.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
math.ln = math.log; local m = {}
local m = {}

-- check whether position (n,c) is free from attacks
local function isplaceok (a, n, c)
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/sieve/lua.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
math.ln = math.log; local m = {}
local m = {}

function m.sieve(N)
local is_prime = {}
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/spectralnorm/lua.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
math.ln = math.log; local m = {}
local m = {}

-- Return A[i][j], for the infinite matrix A
--
Expand Down
5 changes: 0 additions & 5 deletions doc/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ The Pallene compiler has builtins for the following math library functions and c
* math.floor(x)
* math.fmod(x, y)
* math.huge
* math.ln(x)
* math.log(x, base)
* math.maxinteger
* math.mininteger
Expand All @@ -495,9 +494,6 @@ The Pallene compiler has builtins for the following math library functions and c

All the functions currently require float arguments. The compiler will reject integer arguments.

At this time, math.log requires two parameters, and the compiler will reject one parameter calls.
So as a workaround, use math.ln in place of the one parameter math.log.

math.modf returns two values, an integer and a float. One minor difference from Lua is that Lua
may return a float for the first value if some reason it cannot return an integer, such as if
the result is NaN (which only exists in floating point). In Pallene, the first value will always be
Expand Down Expand Up @@ -610,4 +606,3 @@ As usual, {A} means 0 or more As, and \[A\] means an optional A.
'&' | '~' | '|' | '>>' | '<<' | '..' |
'<' | '<=' | '>' | '>=' | '==' | '~=' |
and | or

36 changes: 30 additions & 6 deletions spec/execution_tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,28 @@ function execution_tests.run(compile_file, backend, _ENV, only_compile)
end)
end)

describe("Optional arguments", function()
compile([[
function m.optarg(x:any, y:any): integer
local x:integer = x or (10 as any)
local y:integer = y or (20 as any)
return x+y
end
function m.call0(): integer
return m.optarg()
end
function m.call1(x:integer): integer
return m.optarg(x)
end
function m.call2(x:integer, y:integer): integer
return m.optarg(x,y)
end
]])
end)

describe("Unary Operators /", function()

local tests = {
Expand Down Expand Up @@ -1616,18 +1638,20 @@ function execution_tests.run(compile_file, backend, _ENV, only_compile)
end)
end)

describe("math.ln builtin", function()
describe("math.log 1-arg builtin", function()
compile([[
function m.natural_log(x: float): float
return math.ln(x)
return math.log(x)
end
]])

it("works on positive numbers", function()
run_test([[
assert(0.0 == test.natural_log(1.0))
assert(0.693 == tonumber(string.format("%.3f", test.natural_log(2.0))))
assert(2.303 == tonumber(string.format("%.3f", test.natural_log(10.0))))
local e = math.exp(1)
assert("0.000" == string.format("%.3f", test.natural_log(1.0 )))
assert("0.693" == string.format("%.3f", test.natural_log(2.0 )))
assert("1.000" == string.format("%.3f", test.natural_log(e )))
assert("2.303" == string.format("%.3f", test.natural_log(10.0)))
]])
end)

Expand All @@ -1646,7 +1670,7 @@ function execution_tests.run(compile_file, backend, _ENV, only_compile)
end)
end)

describe("math.log builtin", function()
describe("math.log 2-arg builtin", function()
compile([[
function m.math_log(x: float, base: float): float
return math.log(x, base)
Expand Down
4 changes: 0 additions & 4 deletions spec/translator_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ end)
local function assert_translation(pallene_code, expected)
assert(compile("__translation_test__.pln", pallene_code))
local contents = assert(util.get_file_contents("__translation_test__.lua"))
-- The introduction of math.ln in Pallene to workaround single param math.log requires emitted
-- Lua code to handle this as well. The current workaround injects "math.ln = math.log; " at
-- the beginning of the emited Lua. This function needs to account for this injection.
expected = "math.ln = math.log; " .. expected
assert.are.same(expected, contents)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/typechecker_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ end)

describe("Table constructor", function()

describe("for arrrays", function()
describe("for arrays", function()

it("must not contain named fields", function()
assert_error([[
Expand Down
3 changes: 1 addition & 2 deletions src/pallene/builtins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ builtins.modules = {
floor = T.Function({ T.Float }, { T.Integer }),
fmod = T.Function({ T.Float, T.Float }, { T.Float }),
exp = T.Function({ T.Float }, { T.Float }),
ln = T.Function({ T.Float }, { T.Float }),
log = T.Function({ T.Float, T.Float }, { T.Float }),
log = T.Function({ T.Float, T.Any }, { T.Float }),
modf = T.Function({ T.Float }, { T.Integer, T.Float }),
pow = T.Function({ T.Float, T.Float }, { T.Float }),
sqrt = T.Function({ T.Float }, { T.Float }),
Expand Down
Loading

0 comments on commit 104733f

Please sign in to comment.