Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions asteroid/modules/patterns.ast
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ __retval__ = ('boolean',str.isalnum(val_s[1]))
with _ do
return false.
end

------------------------------------------------------------------
-- Note: patterns are evaluated in the user context therefore we
-- cannot have module access in patterns, except the patterns module
function _mod2 with n do math@mod(n,2) end
function _isclose with n do math@isclose(toreal(n), 0.0) end
------------------------------------------------------------------
-- Common number sets
------------------------------------------------------------------
Expand All @@ -114,10 +118,10 @@ let neg_real = pattern with %[ (n:%real) if n < 0.0 ]%.
let positive = pattern with %[ n if (n is *patterns@pos_int) or (n is *patterns@pos_real) ]%.
let negative = pattern with %[ n if (n is *patterns@neg_int) or (n is *patterns@neg_real) ]%.
let nat = pattern with %[ (n:%integer) if n >= 0 ]%.
let zero = pattern with %[ n if ((n is %real) and (math@isclose(toreal(n), 0.0)))
let zero = pattern with %[ n if ((n is %real) and patterns@_isclose(n))
or ((n is %integer) and (tointeger(n) == 0)) ]%.
let odd = pattern with %[ (n:%integer) if math@mod(n,2) == 1 ]%.
let even = pattern with %[ (n:%integer) if math@mod(n,2) == 0 ]%.
let odd = pattern with %[ (n:%integer) if patterns@_mod2(n) == 1 ]%.
let even = pattern with %[ (n:%integer) if patterns@_mod2(n) == 0 ]%.
------------------------------------------------------------------
-- Containers
------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions asteroid/test-suites/ref-programs/io.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- test module for test ref-programs/prgm-007.ast

load system io.

function output with a do
io @println a.
end

2 changes: 1 addition & 1 deletion asteroid/test-suites/ref-programs/pick.ast
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load system io.
load system pick.

let po = pick @pick([1 to 10]).
let po = pick @pick [1 to 10].
let objects = po @pickitems 3.
io @println objects.

11 changes: 11 additions & 0 deletions asteroid/test-suites/ref-programs/prgm-001.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

let i = 0.

loop
let i = i+1.
if i==10 do
break.
end
end

assert (i==10).
5 changes: 5 additions & 0 deletions asteroid/test-suites/ref-programs/prgm-002.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

function inc with i do
i+1.
end

10 changes: 10 additions & 0 deletions asteroid/test-suites/ref-programs/prgm-003.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

let tuple_list = [
(0,"duck"),
(1,"chicken"),
(2,"turkey")
].

for (1,bird) in tuple_list do
assert(bird is "chicken").
end
7 changes: 7 additions & 0 deletions asteroid/test-suites/ref-programs/prgm-004.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

function sign
with x if x >= 0 do
return 1.
with x if x < 0 do
return -1.
end
11 changes: 11 additions & 0 deletions asteroid/test-suites/ref-programs/prgm-005.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

let x = 0.

function foo with none do
global x.
let x = 1.
end

assert(x==0).
foo().
assert(x==1).
2 changes: 2 additions & 0 deletions asteroid/test-suites/ref-programs/prgm-006-io.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
10

15 changes: 15 additions & 0 deletions asteroid/test-suites/ref-programs/prgm-006.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

load system io.

let x = tointeger (io @input "Please enter an integer: ").

if x < 0 do
io @println "Negative".
elif x == 0 do
io @println "Zero".
elif x == 1 do
io @println "One".
else do
io @println "Positive".
end

5 changes: 5 additions & 0 deletions asteroid/test-suites/ref-programs/prgm-007.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

load "ref-programs/io.ast". -- load my IO module
load system io as systemio. -- load the system IO module and rename it to systemio
io @output "Foobar".
systemio @println "Hello World!".
10 changes: 10 additions & 0 deletions asteroid/test-suites/ref-programs/prgm-008.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

match (1,2)
with (x,y) if x > y do
let x = "GT".
with (x,y) if x < y do
let x = "LT".
with _ do
throw Error("not a valid tuple").
end
assert(x == "LT").
9 changes: 9 additions & 0 deletions asteroid/test-suites/ref-programs/prgm-009.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

load system io.

let l = ["bmw", "volkswagen", "mercedes"].

repeat
let [element|l] = l.
io @println element.
until l is [].
9 changes: 9 additions & 0 deletions asteroid/test-suites/ref-programs/prgm-010.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- define a structure of type A
structure A with
data a.
data b.
end

let obj = A(1,2). -- call default constructor
assert( obj@a == 1 ). -- access first data member
assert( obj@b == 2 ). -- access second data member
18 changes: 18 additions & 0 deletions asteroid/test-suites/ref-programs/prgm-011.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- define a structure of type Person
structure Person with
data name.
data age.
function __init__ with (name:%string,age:%integer) do -- constructor
let this@name = name.
let this@age = age.
end
function __str__ with none do
return this @name+" is "+ tostring(this@age) +" years old".
end
end

let betty = Person("Betty",21). -- call constructor
assert( betty@name == "Betty" ).
assert( betty@age == 21 ).

assert(tostring betty is "Betty is 21 years old").
7 changes: 7 additions & 0 deletions asteroid/test-suites/ref-programs/prgm-012.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load system io.

try
let x = 1/0.
catch Exception("ArithmeticError", s) do
io @println s.
end
8 changes: 8 additions & 0 deletions asteroid/test-suites/ref-programs/prgm-013.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load system io.

let i = 10.

while i >= 0 do
io @println i.
let i = i-1.
end
7 changes: 7 additions & 0 deletions asteroid/test-suites/ref-programs/prgm-014.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
structure A with
data a.
data b.
end

let obj = A(1,2).
assert( obj@a == 1 ). -- access member a
11 changes: 11 additions & 0 deletions asteroid/test-suites/ref-programs/prgm-015.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load system io.

if (1,2) is (x,y) do
io @println "success".
assert(isdefined "x").
assert(isdefined "y").
else
io @println "not matched".
assert(not isdefined "x").
assert(not isdefined "y").
end
2 changes: 1 addition & 1 deletion asteroid/test-suites/regression-tests/test012.ast
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

-- Palindrome list filter
load io.
load system io.

function filter
with (x:%string) if x @explode() == x @explode() @reverse() do
Expand Down
2 changes: 1 addition & 1 deletion asteroid/test-suites/regression-tests/test013.ast
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

load io.
load system io.
let i:%integer = 1.
io @print i.
2 changes: 1 addition & 1 deletion asteroid/test-suites/regression-tests/test015.ast
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,4 @@ catch _ do
io @println("FAILED.").
throw Error("FAIL: unexpected error").
end.
--------------------------------------------------END----
--------------------------------------------------END----
Loading