├── docs ├── requirements.txt ├── moving-avg.png ├── asteroid-small.png ├── header.txt ├── index.rst ├── generate_docs.py ├── Installing and Running.rst └── conf.py ├── asteroid ├── test-suites │ ├── programs │ │ ├── fact-io.txt │ │ ├── calc-io.txt │ │ ├── guess-io.txt │ │ ├── escape.ast │ │ ├── formatted.ast │ │ ├── hello.ast │ │ ├── simple.ast │ │ ├── test_hash.ast │ │ ├── names.csv │ │ ├── cls.ast │ │ ├── stream.ast │ │ ├── people.ast │ │ ├── qsort.ast │ │ ├── fact.ast │ │ ├── largest.ast │ │ ├── teams.ast │ │ ├── dogs.ast │ │ ├── lexer.ast │ │ ├── guess.ast │ │ ├── boom.ast │ │ └── tokenizer.ast │ ├── action-tests │ │ ├── test003-io.txt │ │ ├── test004-io.txt │ │ ├── test005-io.txt │ │ ├── test047-io.txt │ │ ├── test001.ast │ │ ├── test002.ast │ │ ├── test007.ast │ │ ├── test006.ast │ │ ├── test005.ast │ │ ├── test023.ast │ │ ├── test004.ast │ │ ├── test003.ast │ │ ├── test048.ast │ │ ├── test056.ast │ │ ├── test057.ast │ │ ├── test024.ast │ │ ├── test016.ast │ │ ├── test025.ast │ │ ├── test052.ast │ │ ├── test012.ast │ │ ├── test051.ast │ │ ├── test028.ast │ │ ├── test037.ast │ │ ├── test055.ast │ │ ├── test060.ast │ │ ├── test065.ast │ │ ├── test067.ast │ │ ├── test072.ast │ │ ├── test010.ast │ │ ├── test073.ast │ │ ├── test029.ast │ │ ├── test064.ast │ │ ├── test059.ast │ │ ├── test077.ast │ │ ├── test009.ast │ │ ├── test061.ast │ │ ├── test066.ast │ │ ├── test030.ast │ │ ├── test031.ast │ │ ├── test041.ast │ │ ├── test069.ast │ │ ├── test027.ast │ │ ├── test062.ast │ │ ├── test032.ast │ │ ├── test042.ast │ │ ├── test040.ast │ │ ├── test075.ast │ │ ├── test050.ast │ │ ├── test071.ast │ │ ├── test070.ast │ │ ├── test081.ast │ │ ├── test063.ast │ │ ├── test008.ast │ │ ├── test049.ast │ │ ├── test076.ast │ │ ├── test074.ast │ │ ├── test036.ast │ │ ├── test019.ast │ │ ├── test039.ast │ │ ├── test026.ast │ │ ├── test082.ast │ │ ├── test033.ast │ │ ├── test058.ast │ │ ├── test047.ast │ │ ├── test020.ast │ │ ├── test038.ast │ │ ├── test043.ast │ │ ├── test034.ast │ │ ├── test046.ast │ │ ├── test014.ast │ │ ├── test018.ast │ │ ├── test011.ast │ │ ├── test013.ast │ │ ├── test044.ast │ │ ├── test079.ast │ │ ├── test021.ast │ │ ├── test053.ast │ │ ├── test035.ast │ │ ├── test080.ast │ │ ├── test015.ast │ │ ├── test054.ast │ │ ├── test078.ast │ │ ├── test045.ast │ │ ├── test068.ast │ │ └── test017.ast │ ├── ref-programs │ │ ├── prgm-006-io.txt │ │ ├── type.ast │ │ ├── prgm-002.ast │ │ ├── io.ast │ │ ├── vector.ast │ │ ├── prgm-013.ast │ │ ├── pick.ast │ │ ├── math.ast │ │ ├── prgm-004.ast │ │ ├── prgm-012.ast │ │ ├── prgm-014.ast │ │ ├── sort.ast │ │ ├── prgm-001.ast │ │ ├── prgm-005.ast │ │ ├── stream.ast │ │ ├── prgm-007.ast │ │ ├── prgm-009.ast │ │ ├── patterns.ast │ │ ├── prgm-003.ast │ │ ├── prgm-008.ast │ │ ├── prgm-015.ast │ │ ├── prgm-010.ast │ │ ├── prgm-006.ast │ │ ├── prgm-011.ast │ │ └── format.ast │ ├── regression-tests │ │ ├── test067-io.txt │ │ ├── test068-io.txt │ │ ├── test039.ast │ │ ├── test084.ast │ │ ├── test116-io.txt │ │ ├── test092.ast │ │ ├── test144.ast │ │ ├── test086.ast │ │ ├── test001.ast │ │ ├── test013.ast │ │ ├── test037.ast │ │ ├── test025.ast │ │ ├── test093.ast │ │ ├── test154.ast │ │ ├── test021.ast │ │ ├── test142.ast │ │ ├── test020.ast │ │ ├── test016.ast │ │ ├── test038.ast │ │ ├── test079.ast │ │ ├── test004.ast │ │ ├── test122.ast │ │ ├── test003.ast │ │ ├── test017.ast │ │ ├── test030.ast │ │ ├── test035.ast │ │ ├── test052.ast │ │ ├── test136.ast │ │ ├── test036.ast │ │ ├── test080.ast │ │ ├── test112.ast │ │ ├── test011.ast │ │ ├── test014.ast │ │ ├── test146.ast │ │ ├── test068.ast │ │ ├── test087.ast │ │ ├── test005.ast │ │ ├── test006.ast │ │ ├── test009.ast │ │ ├── test063.ast │ │ ├── test091.ast │ │ ├── test044.ast │ │ ├── test076.ast │ │ ├── test123.ast │ │ ├── test125.ast │ │ ├── test152.ast │ │ ├── test045.ast │ │ ├── test047.ast │ │ ├── test058.ast │ │ ├── test050.ast │ │ ├── test090.ast │ │ ├── test105.ast │ │ ├── test107.ast │ │ ├── test059.ast │ │ ├── test061.ast │ │ ├── test022.ast │ │ ├── test032.ast │ │ ├── test023.ast │ │ ├── test082.ast │ │ ├── test097.ast │ │ ├── test102.ast │ │ ├── test128.ast │ │ ├── test155.ast │ │ ├── test075.ast │ │ ├── test106.ast │ │ ├── test108.ast │ │ ├── test034.ast │ │ ├── test111.ast │ │ ├── test055.ast │ │ ├── test077.ast │ │ ├── test026.ast │ │ ├── test110.ast │ │ ├── test024.ast │ │ ├── test067.ast │ │ ├── test094.ast │ │ ├── test100.ast │ │ ├── test140.ast │ │ ├── test156.ast │ │ ├── test029.ast │ │ ├── test070.ast │ │ ├── test083.ast │ │ ├── test057.ast │ │ ├── test028.ast │ │ ├── test033.ast │ │ ├── test104.ast │ │ ├── test141.ast │ │ ├── test018.ast │ │ ├── test139.ast │ │ ├── test046.ast │ │ ├── test126.ast │ │ ├── test153.ast │ │ ├── test071.ast │ │ ├── test121.ast │ │ ├── test078.ast │ │ ├── test095.ast │ │ ├── test049.ast │ │ ├── test041.ast │ │ ├── test135.ast │ │ ├── test012.ast │ │ ├── test051.ast │ │ ├── test053.ast │ │ ├── test074.ast │ │ ├── test118.ast │ │ ├── test117.ast │ │ ├── test073.ast │ │ ├── test072.ast │ │ ├── test120.ast │ │ ├── test137.ast │ │ ├── test081.ast │ │ ├── test103.ast │ │ ├── test088.ast │ │ ├── test027.ast │ │ ├── test138.ast │ │ ├── test031.ast │ │ ├── test066.ast │ │ ├── test101.ast │ │ ├── test124.ast │ │ ├── test019.ast │ │ ├── test010.ast │ │ ├── test114.ast │ │ ├── test113.ast │ │ ├── test132.ast │ │ ├── test043.ast │ │ ├── test129.ast │ │ ├── test042.ast │ │ ├── test143.ast │ │ ├── test060.ast │ │ ├── test064.ast │ │ ├── test147.ast │ │ ├── test065.ast │ │ ├── test008.ast-not-valid │ │ ├── test115.ast │ │ ├── test119.ast │ │ ├── test040.ast │ │ ├── test007.ast │ │ ├── test109.ast │ │ ├── test116.ast │ │ ├── test134.ast │ │ └── test127.ast │ ├── ug-programs │ │ ├── prgm-024-io.txt │ │ ├── prgm-053-io.txt │ │ ├── prgm-054-io.txt │ │ ├── prgm-008b.ast │ │ ├── prgm-019.ast │ │ ├── prgm-001.ast │ │ ├── prgm-002.ast │ │ ├── prgm-004.ast │ │ ├── prgm-008.ast │ │ ├── prgm-006.ast │ │ ├── prgm-027.ast │ │ ├── prgm-007.ast │ │ ├── prgm-005.ast │ │ ├── prgm-022.ast │ │ ├── prgm-021.ast │ │ ├── prgm-019a.ast │ │ ├── prgm-020.ast │ │ ├── prgm-051.ast │ │ ├── prgm-053.ast │ │ ├── prgm-009.ast │ │ ├── prgm-009a.ast │ │ ├── prgm-041.ast │ │ ├── prgm-049.ast │ │ ├── prgm-010.ast │ │ ├── prgm-045.ast │ │ ├── prgm-046.ast │ │ ├── prgm-048.ast │ │ ├── prgm-050.ast │ │ ├── prgm-055.ast │ │ ├── prgm-008a.ast │ │ ├── prgm-008c.ast │ │ ├── prgm-054.ast │ │ ├── prgm-047.ast │ │ ├── prgm-025b.ast │ │ ├── prgm-012.ast │ │ ├── prgm-015.ast │ │ ├── prgm-043.ast │ │ ├── prgm-014.ast │ │ ├── prgm-025a.ast │ │ ├── prgm-034.ast │ │ ├── prgm-016.ast │ │ ├── prgm-029.ast │ │ ├── prgm-023.ast │ │ ├── prgm-017a.ast │ │ ├── prgm-018.ast │ │ ├── prgm-035.ast │ │ ├── prgm-028.ast │ │ ├── prgm-031a.ast │ │ ├── prgm-011.ast │ │ ├── prgm-024.ast │ │ ├── prgm-031.ast │ │ ├── prgm-032.ast │ │ ├── prgm-036.ast │ │ ├── prgm-043a.ast │ │ ├── prgm-003.ast │ │ ├── prgm-038.ast │ │ ├── prgm-039.ast │ │ ├── prgm-033.ast │ │ ├── prgm-030.ast │ │ ├── prgm-037.ast │ │ ├── prgm-017.ast │ │ ├── prgm-026.ast │ │ ├── prgm-052.ast │ │ └── prgm-044.ast │ ├── python-tests │ │ ├── README.md │ │ └── repl_test.py │ ├── redundant-pattern-test │ │ ├── test003.ast │ │ ├── test012.ast │ │ ├── test011.ast │ │ ├── test001.ast │ │ ├── test005.ast │ │ ├── test002.ast │ │ ├── test010.ast │ │ ├── test006.ast │ │ ├── test008.ast │ │ ├── test009.ast │ │ ├── test004.ast │ │ ├── test027.ast │ │ ├── test028.ast │ │ └── test026.ast │ └── run-tests.py ├── version.py ├── modules │ ├── sort.ast │ ├── pick.ast │ ├── random.ast │ ├── vector.ast │ ├── set.ast │ └── stream.ast └── state.py ├── .replit ├── asteroid-small.png ├── requirements.txt ├── .gitignore ├── pyproject.toml ├── start ├── .github └── workflows │ ├── tests.yml │ └── generate_documentation.yml ├── LICENSE ├── .readthedocs.yaml ├── setup.py └── CONTRIBUTING.md /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | sphinx_rtd_theme 2 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/fact-io.txt: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /.replit: -------------------------------------------------------------------------------- 1 | language = "bash" 2 | run = "bash start" 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/calc-io.txt: -------------------------------------------------------------------------------- 1 | (2+3)*2 2 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test003-io.txt: -------------------------------------------------------------------------------- 1 | Leo 2 | 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test004-io.txt: -------------------------------------------------------------------------------- 1 | Leo 2 | 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test005-io.txt: -------------------------------------------------------------------------------- 1 | Leo 2 | 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test047-io.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/prgm-006-io.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test067-io.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-024-io.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-053-io.txt: -------------------------------------------------------------------------------- 1 | Leo 2 | 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-054-io.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test068-io.txt: -------------------------------------------------------------------------------- 1 | Leo 2 | 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test039.ast: -------------------------------------------------------------------------------- 1 | 2 | let () = none. 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test084.ast: -------------------------------------------------------------------------------- 1 | 2 | let 1 = 1. 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-008b.ast: -------------------------------------------------------------------------------- 1 | let x = 1.1 + toreal(1). -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-019.ast: -------------------------------------------------------------------------------- 1 | let %integer = 1. 2 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/guess-io.txt: -------------------------------------------------------------------------------- 1 | Leo 2 | 10 3 | 5 4 | 3 5 | 4 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test116-io.txt: -------------------------------------------------------------------------------- 1 | Leo 2 | Blue 3 | 4 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-001.ast: -------------------------------------------------------------------------------- 1 | let [x,2,y] = [1,2,3]. 2 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-002.ast: -------------------------------------------------------------------------------- 1 | let [1,2,3] = [1,2,3]. 2 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test092.ast: -------------------------------------------------------------------------------- 1 | 2 | let true = 3 in [1,2,3]. 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/escape.ast: -------------------------------------------------------------------------------- 1 | escape 2 | " 3 | print('hello world') 4 | ". 5 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test144.ast: -------------------------------------------------------------------------------- 1 | let a = eval "1+1". 2 | assert(a == 2). 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-004.ast: -------------------------------------------------------------------------------- 1 | let x:%string = "value: " + tostring 1. 2 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-008.ast: -------------------------------------------------------------------------------- 1 | let none = (). 2 | let () = none. 3 | -------------------------------------------------------------------------------- /asteroid-small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asteroid-lang/asteroid/HEAD/asteroid-small.png -------------------------------------------------------------------------------- /asteroid/test-suites/programs/formatted.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | io @println "hello world!\n". 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-006.ast: -------------------------------------------------------------------------------- 1 | let one_tuple = (1,). -- this is a 1-tuple 2 | -------------------------------------------------------------------------------- /docs/moving-avg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asteroid-lang/asteroid/HEAD/docs/moving-avg.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pandas 2 | numpy 3 | matplotlib 4 | pyreadline3; platform_system == "Windows" 5 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test001.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | io @println "Hello, World!". 4 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/type.ast: -------------------------------------------------------------------------------- 1 | 2 | let i = 1. 3 | assert(gettype(i) == "integer"). 4 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test086.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | io @println (1,2,3). 5 | -------------------------------------------------------------------------------- /docs/asteroid-small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/asteroid-lang/asteroid/HEAD/docs/asteroid-small.png -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test002.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | io @__raw_print__ "Hello, World!". 4 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test007.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | io @println ("Hello!" @length()). 4 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test001.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | let x = 1. 5 | io @println x. 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | .DS_Store 3 | *~ 4 | dist/ 5 | asteroid_lang.egg-info/ 6 | action-test68.png 7 | .vscode/ -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test013.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | let i:%integer = 1. 4 | io @print i. 5 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test037.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | let fst = (1,2)@0. 4 | io @println fst. 5 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/prgm-002.ast: -------------------------------------------------------------------------------- 1 | 2 | function inc with i do 3 | i+1. 4 | end 5 | 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test025.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | let q:%integer = 3. 5 | io @println q. 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test093.ast: -------------------------------------------------------------------------------- 1 | 2 | let true = (1,2) is (1,x). 3 | let true = (1,3) is (1,x). 4 | 5 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test154.ast: -------------------------------------------------------------------------------- 1 | load system io as myio. 2 | 3 | myio @println "Hello, World!". 4 | 5 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-027.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | io @println ((lambda with n do n+1) 1). 4 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test006.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | load "util". 4 | io @println (len("Hello!")). 5 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test021.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | let a:%list = [1,2] . 5 | io @println a. 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test142.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | for c in (1,2,3) do 4 | io @println c. 5 | end 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test020.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | let (x:%integer) if x > 0 = 2. 5 | io @println x. 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-007.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | io @println ("this is my list: " + tostring [1,2,3]). 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test005.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | io @println ("Hello, "+io @input("Enter your name: ")+"!"). 4 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test016.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | let (x,y) if x is y = ([1],[1]). 5 | io @println x. 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test038.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | let t = (1,2). 4 | io @println ("tuple = " + tostring t). 5 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test079.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | for x in 1 to 10 do 5 | io @println x. 6 | end 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-005.ast: -------------------------------------------------------------------------------- 1 | let l = [1,2,3]. -- this is a list 2 | let t = (1,2,3). -- this is a tuple 3 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test004.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | let l if tostring l is "\[.*\]" = [1,2,3]. 4 | io @println l. 5 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test023.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. -- definition of pi 3 | 4 | io @println (math @pi). 5 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/hello.ast: -------------------------------------------------------------------------------- 1 | -- the obligatory 'Hello, World!' program 2 | 3 | load system io. 4 | 5 | io @println "Hello, World!". 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test122.ast: -------------------------------------------------------------------------------- 1 | assert ([1,2,3] @index(2,loc(0)) == 1). 2 | assert ("abcdef" @index("de",loc(0)) == 3). 3 | 4 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-022.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | for i in 0 to 10 step 2 do 4 | io @println i. 5 | end 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/python-tests/README.md: -------------------------------------------------------------------------------- 1 | The tests in this suite are not run when run-tests.py is run. Currently, each file must be run individually. -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test003.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | let digits = "332211" @explode() @sort(). 5 | io @println digits. 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test017.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | let (l:%list) if tostring l is "\[.*2.*\]" = [1,2,3]. 5 | assert(l == [1,2,3]). -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test030.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | let name:"a.*" = "abc". 5 | io @println name. 6 | assert (name is "abc") 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test035.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | let a = []. 4 | 5 | if a is [b|rest] do 6 | io @println "yes". 7 | end 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test052.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | try 5 | let -1 = -1 . 6 | catch e do 7 | io @println e. 8 | end 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test136.ast: -------------------------------------------------------------------------------- 1 | -- test expressions computing return values 2 | let x = (lambda with i do i+1) 1. 3 | assert(x is 2). 4 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-021.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | let x:%real = math @pi. 5 | io @println x. 6 | -------------------------------------------------------------------------------- /docs/header.txt: -------------------------------------------------------------------------------- 1 | .. 2 | /* header for generated .rst files */ 3 | 4 | .. 5 | *** DO NOT EDIT; MACHINE GENERATED *** 6 | 7 | #define hash_comment # 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test004.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let name = io @input("Enter your name: "). 4 | io @println ("Hello, "+name+"!"). 5 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test036.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | let a = [1,2]. 4 | 5 | if a is [b|c|d|rest] do 6 | io @println "yes". 7 | end 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test080.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | for bird in ["turkey","duck","chicken"] do 5 | io @println bird. 6 | end 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test112.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let array = [1,2,3]. 4 | 5 | array @extend((4,5,6)). 6 | io @println(array). 7 | 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-019a.ast: -------------------------------------------------------------------------------- 1 | structure Foo with 2 | data a. 3 | data b. 4 | end 5 | 6 | let %Foo = Foo(1,2). 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-020.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let t:(x,y) = (1,2). -- using a named pattern on lhs 4 | io @println t. 5 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/simple.ast: -------------------------------------------------------------------------------- 1 | -- a simple program using lambda functions 2 | 3 | load system io. 4 | 5 | io @println ((lambda with n do return n+1) 1). 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test011.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | let foo = "abc". 5 | let l = foo @explode() @reverse() @join(""). 6 | io @println l. 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test014.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | try 5 | let (1,y) = pattern (1,x). 6 | catch e do 7 | io @println e. 8 | end 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test146.ast: -------------------------------------------------------------------------------- 1 | let (a,b,c):(%string,%integer,%real) = ("1",2,3.0). 2 | assert(a=="1"). 3 | assert(b==2). 4 | assert(c==3.0). 5 | 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-051.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let a = 1. 4 | let b = 2. 5 | io @println ("a + b = " + tostring (a + b)). 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test068.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | let name = io @input("Please enter your name: "). 5 | io @println("Hello " + name + "!"). 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test087.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | let nl = [[1 to 5]]. 5 | io @println nl. 6 | assert (nl is [[1,2,3,4,5]]). 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-053.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let name = io @input("What is your name? "). 4 | io @println ("Hello " + name + "!"). 5 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test005.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | let r = [1,2,3] @reduce (lambda with (x,y) do return x*y). 4 | io @println r. 5 | assert (r == 6). 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test006.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | let r = [1,2,3] @map (lambda with x do return x+1). 4 | io @println r. 5 | assert (r == [2,3,4]). 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test003.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | io @print ("Enter your name: "). 4 | let name = io @input(). 5 | io @print ("Hello, "+name+"!"). 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/test_hash.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system hash. 3 | 4 | let h = hash @hash(). 5 | h@insert("hello","there"). 6 | io @println (h@get("hello")). 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test009.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | structure A with 4 | data a. 5 | end 6 | 7 | let A(x:%string) = A("hello"). 8 | io @println x. 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test063.ast: -------------------------------------------------------------------------------- 1 | 2 | load "util". 3 | structure S with 4 | data x. 5 | end 6 | 7 | let v = S(S(S(0))). 8 | assert(v is S(S(S(0)))). 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test091.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | let y = [0 to 10 step 2]. 6 | let z = 4 in y. 7 | io @println z. 8 | assert z. 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-009.ast: -------------------------------------------------------------------------------- 1 | let a = [1,2,3]. -- construct list a 2 | let b = [a@2, a@1, a@0]. -- reverse list a 3 | assert (b == [3,2,1]). 4 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test048.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let bin = "101101". 4 | let int = tointeger(bin,2). 5 | io @println int. 6 | 7 | assert (int == 45). 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test056.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let a = [3,5,7,4]. 4 | let (a@2,a@3) = (a@3,a@2). 5 | io @println a. 6 | 7 | assert (a is [3,5,4,7]). 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test057.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let a = [10, 20, 30, 40, 50]. 4 | io @println (a @reverse()). 5 | 6 | assert(a == [50,40,30,20,10]). 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test044.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | let slice = [4 to 0 step -1]. 5 | io @println slice. 6 | assert(slice is [4,3,2,1,0]). 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test076.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | let y = -1. 6 | let x = 4 if y == 3 else 0. 7 | io @println x. 8 | assert (x == 0). 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test123.ast: -------------------------------------------------------------------------------- 1 | 2 | structure A with 3 | data a. 4 | data b. 5 | end 6 | 7 | let o = A(1,2). 8 | 9 | 10 | let *o = o. 11 | 12 | 13 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test125.ast: -------------------------------------------------------------------------------- 1 | -- test pattern match constraints on indexes 2 | 3 | let q = [1,2,3]. 4 | let q@1:%integer = 0. 5 | 6 | assert( q == [1,0,3] ). 7 | 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test152.ast: -------------------------------------------------------------------------------- 1 | load system os. 2 | load system io. 3 | 4 | if os @platform == "darwin" do 5 | io @println (os @env @get "HOME"). 6 | end 7 | 8 | 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test024.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | let tau = 2.0 * math @pi. 5 | 6 | io @println (math @e). 7 | io @println tau. 8 | 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/io.ast: -------------------------------------------------------------------------------- 1 | -- test module for test ref-programs/prgm-007.ast 2 | 3 | load system io. 4 | 5 | function output with a do 6 | io @println a. 7 | end 8 | 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/vector.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system vector. 3 | 4 | let a = [1,0]. 5 | let b = [0,1]. 6 | 7 | io @println (vector @dot (a,b)). 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test045.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | let a = [[1,2],[3,4],[5,6]]. 6 | let l = (a @0). 7 | io @println l. 8 | assert(l is [1,2]). 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test047.ast: -------------------------------------------------------------------------------- 1 | 2 | load system random. 3 | load system io. 4 | 5 | let v = random @random(). 6 | 7 | io @println v. 8 | assert(v <= 1.0 and v >= 0.0). 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test058.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | try 5 | let i = 10/0. 6 | io @println i. 7 | catch e do 8 | io @println e. 9 | end 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-009a.ast: -------------------------------------------------------------------------------- 1 | let a = [1,2,3]. -- construct list a 2 | let b = a@[2,1,0]. -- reverse list a using slice [2,1,0] 3 | assert (b == [3,2,1]). 4 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test016.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | for n in 0 to 5 do 4 | io @println (tostring n + " file" + ("s " if n>1 or n==0 else " ") + "found"). 5 | end 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/prgm-013.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let i = 10. 4 | 5 | while i >= 0 do 6 | io @println i. 7 | let i = i-1. 8 | end 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test050.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | try 5 | let 2 = 1 + 1. 6 | catch _ do 7 | io @println "pattern match failed". 8 | end 9 | 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test090.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | let a = [0,1,2,3]. 6 | let b = a@[0 to 3 step 2]. 7 | io @println b. 8 | assert (b is [0,2]). 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test105.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | function ident with n do return n end 6 | io @println (ident(2)). 7 | assert (ident(2) is 2). 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test107.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | io @println ((lambda with n do return n+1) 1). 6 | assert ((lambda with n do return n+1) 1 is 2). 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-041.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | let P = pattern ([a],[b]). 3 | let v = ([1],[2]). 4 | let *P = v. 5 | io @println a. 6 | io @println b. 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-049.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | try 4 | let (x,y) = (1,2,3). 5 | catch _ do 6 | io @println "something happened". 7 | end 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/pick.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system pick. 3 | 4 | let po = pick @pick [1 to 10]. 5 | let objects = po @pickitems 3. 6 | io @println objects. 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test059.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | try 5 | let i = 10/0. 6 | io @println i. 7 | catch Exception(_, v) do 8 | io @println v. 9 | end 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test061.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | let l = [1,2,3]. 5 | 6 | repeat do 7 | let [head|l] = l. 8 | io @println head. 9 | until l is []. 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-010.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let a = [1,2,3]. 4 | let b = a @reverse (). -- reverse list using member function 'reverse' 5 | io @println b. 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-045.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | try 4 | let x = 1/0. 5 | catch Exception("ArithmeticError", s) do 6 | io @println s. 7 | end 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test025.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let n = 3. 4 | let fact = [1 to n] @reduce(lambda with (a,b) do return a*b). 5 | io @println fact. 6 | assert (fact == 6). 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test052.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let a = tointeger ([67, 8, 1, 5, 45] @map tostring @sort true @join ""). 4 | io @println a. 5 | 6 | assert (a == 8675451). 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/math.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | let x = math @sin( math @pi / 2.0 ). 5 | io @println("The sine of pi / 2 is " + tostring x + "."). 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/prgm-004.ast: -------------------------------------------------------------------------------- 1 | 2 | function sign 3 | with x if x >= 0 do 4 | return 1. 5 | with x if x < 0 do 6 | return -1. 7 | end 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-046.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | try 4 | throw Error("something worth throwing"). 5 | catch Error(s) do 6 | io @println s. 7 | end 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-048.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | try 4 | throw ("funny exception", 42). 5 | catch ("funny exception", v) do 6 | io @println v. 7 | end 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test012.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let root = "file". 4 | let ext = ".txt". 5 | 6 | for i in 1 to 5 do 7 | io @println (root + tostring i + ext). 8 | end 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test051.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let bits = "1010101" @replace("0","") 4 | @length(). 5 | io @println bits. 6 | 7 | assert (bits == 4). 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/prgm-012.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | try 4 | let x = 1/0. 5 | catch Exception("ArithmeticError", s) do 6 | io @println s. 7 | end 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test022.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | structure A with 4 | data a. 5 | data b. 6 | end 7 | 8 | let a = A(1,2). 9 | let v:%A = a. 10 | io @println v. 11 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test032.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | function addc with x do return lambda with y do return x + y end 5 | let a = (addc 1) 1. 6 | io @println a. 7 | assert(a is 2). 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-050.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | try 4 | let (x,y) = (1,2,3). 5 | catch e do 6 | io @println ("something happened: "+ tostring e). 7 | end 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-055.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | let x = math @sin( math@pi / 2.0 ). 5 | io @println("The sine of pi / 2 is " + tostring x + "."). 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test028.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let sq = [1 to 10] @map(lambda with x do return x*x). 4 | 5 | io @println sq. 6 | 7 | assert (sq == [1,4,9,16,25,36,49,64,81,100]) 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/names.csv: -------------------------------------------------------------------------------- 1 | names, 2 | "Sofia", 3 | "Andrew", 4 | "Evan", 5 | "Patrick", 6 | "Julio", 7 | "Zachary", 8 | "Joshua", 9 | "Emily", 10 | "Samantha", 11 | "Timothy", 12 | "Shannon", 13 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/prgm-014.ast: -------------------------------------------------------------------------------- 1 | structure A with 2 | data a. 3 | data b. 4 | end 5 | 6 | let obj = A(1,2). 7 | assert( obj@a == 1 ). -- access member a 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test023.ast: -------------------------------------------------------------------------------- 1 | 2 | -- load system io. 3 | structure A with 4 | data a. 5 | data b. 6 | end 7 | 8 | let a = A(1,2). 9 | assert((a @a is 1) and (a @b is 2)). 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test037.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | try 4 | io @println (42/0). 5 | catch Exception(_,m) do 6 | io @println m. 7 | end 8 | io @println "We are still alive...". 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test055.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let (a,b) = (10,20). 4 | let (b,a) = (a,b). 5 | io @println ("a = "+ tostring a,"b = "+ tostring b). 6 | 7 | assert ((a,b) is (20,10)). 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test082.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | let x = 1. 6 | while x <= 10 do 7 | io @println x. 8 | let x = x + 1. 9 | end 10 | assert (x == 11). 11 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test097.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | let a = [1,2,3]. 6 | let b = [0,0,0]. 7 | let [b@2,b@1,b@0] = a. 8 | io @println b. 9 | assert (b is [3,2,1]). 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test102.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | -- reverse the list 6 | let a = [1,2,3]. 7 | let a = a@[2,1,0]. 8 | io @println a. 9 | assert (a is [3,2,1]). 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test128.ast: -------------------------------------------------------------------------------- 1 | -- testing function calls within patterns 2 | 3 | function goo with none do 4 | return pattern x. 5 | end 6 | 7 | let *goo() = 4. 8 | assert (x==4). 9 | 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test155.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | if toplevel() do 4 | io @println "Hello, World!". 5 | else do 6 | throw Error "toplevel operator did not work". 7 | end 8 | 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test060.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system vector. 3 | 4 | let a = [1 to 10]. 5 | let b = vector @add(a,1). 6 | io @println b. 7 | 8 | assert(b == [2,3,4,5,6,7,8,9,10,11]). 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test075.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load system util. 4 | 5 | let [h|t] = [1,2,3]. 6 | io @println ("head: " + tostring h + " tail: " + tostring t). 7 | assert(h == 1 and t == [2,3]). 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test106.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | let x = 1. 5 | function ident with n do return n end. 6 | io @println (ident( ident (x))) . 7 | assert (ident( ident (x)) is 1) . 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test108.ast: -------------------------------------------------------------------------------- 1 | -- Testing relational operators on booleans 2 | -- == 3 | assert(true == true). 4 | assert(false == false). 5 | 6 | -- =/= 7 | assert(true =/= false). 8 | assert(false =/= true). -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test065.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let a = [4, 6, 8, 1, 0, 58, 1, 34, 7, 4, 2]. 4 | let s = a @reduce(lambda with (x,y) do return x+y). 5 | io @println s. 6 | 7 | assert (s == 125). 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/sort.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system sort. 3 | let sl = sort @sort((lambda with (x,y) do true if x=42", 4 | "wheel", 5 | "pandas", 6 | "numpy", 7 | "matplotlib", 8 | "pyreadline3" 9 | ] 10 | build-backend = "setuptools.build_meta" 11 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test067.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let a = [7, 11, 34, 50, 200]. 4 | let avg = a @reduce(lambda with (x,y) do return x+y)/a @length(). 5 | io @println avg. 6 | 7 | assert (avg == 60). 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test055.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | let cl = 1 + 2. 6 | let cr = 3. 7 | let p = cl + cr. 8 | 9 | io @println (1+2+3 is *p). 10 | assert ((1+2+3 is *p) is true). 11 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test077.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | for x in 0 to 10 do 6 | io @println x. 7 | if x == 5 do 8 | break. 9 | end 10 | end 11 | assert (x == 5). 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-008a.ast: -------------------------------------------------------------------------------- 1 | 2 | -- define a function 3 | function inc with x do 4 | return x+1. 5 | end 6 | 7 | -- show that 'inc' is of type 'function' 8 | assert (gettype(inc) == "function"). -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-008c.ast: -------------------------------------------------------------------------------- 1 | -- define a first-class pattern 2 | let p = pattern (x:%integer) if x>0. 3 | 4 | -- check the type of the value stored in p 5 | assert (gettype(p) == "pattern"). 6 | 7 | 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-054.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let i if i > 0 = tointeger(io @input("Please enter a positive integer value: ")). 4 | 5 | for k in 1 to i do 6 | io @println k. 7 | end 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test072.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | let array = [20 to 30] @filter(lambda with x do math @mod(x,2)=/=0). 5 | io @println array. 6 | 7 | assert (array == [21,23,25,27,29]). 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/prgm-001.ast: -------------------------------------------------------------------------------- 1 | 2 | let i = 0. 3 | 4 | loop 5 | let i = i+1. 6 | if i==10 do 7 | break. 8 | end 9 | end 10 | 11 | assert (i==10). 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test026.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | function ident with i do return i end 5 | 6 | try 7 | let p = - ident(1). 8 | io @println p. 9 | catch e do 10 | io @println e. 11 | end 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test110.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | structure A with 4 | data x. 5 | end 6 | 7 | assert(A(0) is %A). 8 | assert(3 is %integer). 9 | assert(not(3 is %A)). 10 | assert(not(A(0) is %integer)). -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-047.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | try 4 | throw Exception("MyException","something worth throwing"). 5 | catch Exception("MyException",s) do 6 | io @println s. 7 | end 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test010.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | let str = " Hello , World ! " @trim() 3 | @replace(" ",""). 4 | io @println str. 5 | assert(str == "Hello, World!"). 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test073.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | let a = [20 to 30]. 5 | let array = a @[1 to a @length()-1 step 2] . 6 | io @println array. 7 | 8 | assert (array == [21,23,25,27,29]). 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test024.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | function ident with i do return i end 5 | 6 | try 7 | let p = not ident(1). 8 | io @println p. 9 | catch e do 10 | io @println e. 11 | end 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test067.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | let cnt = tointeger(io @input("Please enter an integer value: ")). 5 | 6 | for i in 1 to cnt do 7 | io @println i. 8 | end 9 | assert (i is cnt). 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test029.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | let p2 = [0 to 9] @map(lambda with x do return math @pow(2,x)). 5 | 6 | io @println p2. 7 | 8 | assert (p2 == [1,2,4,8,16,32,64,128,256,512]) 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test064.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system set. 3 | 4 | let a = [1 to 10]. 5 | let b = [5 to 15]. 6 | let c = set @diff(a,b). 7 | io @println c. 8 | 9 | assert(c @sort() == [1,2,3,4]). 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-025b.ast: -------------------------------------------------------------------------------- 1 | function reduce with (a,b) do -- pattern match the actual argument 2 | return a*b. 3 | end 4 | 5 | let r = reduce (2,4). -- function call via juxtaposition 6 | assert (r == 8). 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test059.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system random. 3 | 4 | random @seed(42). 5 | let b = [1 to 20] @shuffle(). 6 | io @println b. 7 | 8 | assert(b == [20,6,15,5,10,14,16,19,7,13,18,11,2,12,3,17,8,9,1,4]). 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test077.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let a = [3, 7, 19, 20, 34]. 4 | let b = a @reduce(lambda with ((_,x),y) do (true,y) if x<=y else (false,y),(true,a@0)). 5 | 6 | io @println (b@0). 7 | 8 | assert (b@0). 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/prgm-005.ast: -------------------------------------------------------------------------------- 1 | 2 | let x = 0. 3 | 4 | function foo with none do 5 | global x. 6 | let x = 1. 7 | end 8 | 9 | assert(x==0). 10 | foo(). 11 | assert(x==1). 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/stream.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system stream. 3 | 4 | let s = stream @stream [1 to 10]. 5 | while not s @eof() do 6 | io @print (tostring (s @get()) + " "). 7 | end 8 | io @println "". 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test094.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | let b = [[1,2,3], 6 | [4,5,6], 7 | [7,8,9]]. 8 | let b@1@1 = 0. 9 | io @println b. 10 | assert (b is [[1,2,3],[4,0,6],[7,8,9]]). 11 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test100.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | function inc with n do return n+1 end 6 | 7 | let v = inc(inc(0)). 8 | let q = 1 + 1 + 1. 9 | io @println (v, q). 10 | assert ((v, q) is (2,3)). 11 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test140.ast: -------------------------------------------------------------------------------- 1 | -- test binding terms 2 | let Pair = pattern %[(x,y)]%. 3 | 4 | let *Pair bind [x as a, y] = (1,2). -- bindings of the variables x and y are now visible again 5 | assert( a == 1). 6 | assert(y == 2). 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test156.ast: -------------------------------------------------------------------------------- 1 | structure A with 2 | data a. 3 | function __init__ with _ do 4 | let this@a = 2. 5 | end 6 | end 7 | 8 | -- testing objects as patterns 9 | let o = A(1). 10 | let *o = A(1). 11 | 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test009.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let str = "Hello, World!" @explode() 4 | @reverse() 5 | @join(""). 6 | io @println str. 7 | assert(str == "!dlroW ,olleH"). 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/prgm-007.ast: -------------------------------------------------------------------------------- 1 | 2 | load io. -- load my IO module 3 | load system io as systemio. -- load the system IO module and rename it to systemio 4 | io @output "Foobar". 5 | systemio @println "Hello World!". 6 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/prgm-009.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | let l = ["bmw", "volkswagen", "mercedes"]. 5 | 6 | repeat 7 | let [element|l] = l. 8 | io @println element. 9 | until l is []. 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test029.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load system hash. 4 | 5 | let h = hash @hash(). 6 | h @insert("hello","there"). 7 | let result = h @get("hello"). 8 | io @println (result). 9 | assert (result is "there"). 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test070.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | function ident 6 | with n do 7 | return n 8 | end 9 | 10 | let y = ident ident 0. 11 | 12 | io @println y. 13 | assert (y is 0). 14 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test083.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | let x = 1. 6 | loop 7 | if x > 10 do 8 | break. 9 | end 10 | io @println x. 11 | let x = x + 1. 12 | end 13 | assert (x == 11). 14 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-012.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | let a = [1 to 10] @map (lambda with x do math @mod (x,2)) 5 | @map (lambda with x do 1 if x==1 else -1). 6 | 7 | io @println a. 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-015.ast: -------------------------------------------------------------------------------- 1 | -- build a list of tuples 2 | let b = [("a","b","c"), 3 | ("d","e","f"), 4 | ("g","h","i")]. 5 | -- Access an element in the nested structure. 6 | assert (b@0@1 == "b"). 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-043.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let pos_int = pattern %[v if (v is %integer) and (v > 0)]%. 4 | 5 | function add with (a:*pos_int,b:*pos_int) do 6 | return a+b. 7 | end 8 | 9 | io @println (add(1,2)). -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test061.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system vector. 3 | 4 | let a = [10 to 20]. 5 | let b = [30 to 40]. 6 | let c = vector @add(a,b). 7 | io @println c. 8 | 9 | assert(c == [40,42,44,46,48,50,52,54,56,58,60]). 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test066.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let a = [4, 6, 8, 1, 0, 58, 1, 34, 7, 4, 2]. 4 | let f = (lambda with (x,y) do return x+(y if y > 10 else 0)). 5 | let s = a @reduce(f,0). 6 | io @println s. 7 | 8 | assert (s == 92). 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/patterns.ast: -------------------------------------------------------------------------------- 1 | load system patterns. 2 | 3 | let evens = []. 4 | for num in 1 to 10 do 5 | if num is *patterns@even do 6 | let evens = evens@append(num). 7 | end 8 | end 9 | assert(evens is [2,4,6,8,10]). -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test057.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | structure MyException with 5 | data e. 6 | end 7 | 8 | try 9 | throw MyException("Hello There!"). 10 | catch MyException(v) do 11 | io @println v. 12 | end 13 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-014.ast: -------------------------------------------------------------------------------- 1 | let a = (1,2,3). -- construct tuple a 2 | let b = a@1. -- access the second element in tuple a, tuples are 0-indexed 3 | assert (b == 2). -- assert that the value of the second element is correct 4 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-025a.ast: -------------------------------------------------------------------------------- 1 | function double with i do -- pattern match the actual arg with i 2 | return 2*i. 3 | end 4 | 5 | let d = double 2. -- function call via juxtaposition, no parentheses necessary 6 | assert (d == 4). 7 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test028.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | structure B with 5 | data b. 6 | function get with none do return this @b end 7 | end 8 | 9 | let q = B([1,2,3]). 10 | 11 | io @println (q @get() @length()). 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test033.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | let a = "abcde". 4 | let i = a @1. 5 | io @println i. 6 | let b = a @[0,2,4]. 7 | io @println b. 8 | let i = a @length(). 9 | io @println i. 10 | let e = a @explode(). 11 | io @println e. 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test030.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | let odd = [] 5 | for (n if math @mod(n,2) =/= 0) in 1 to 10 do 6 | let odd = odd + [n]. 7 | end 8 | 9 | io @println odd. 10 | assert(odd == [1,3,5,7,9]) 11 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test031.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | let even = [] 5 | for (n if math @mod(n,2) == 0) in 1 to 10 do 6 | let even = even + [n]. 7 | end 8 | 9 | io @println even. 10 | assert(even == [2,4,6,8,10]) 11 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test041.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | let x = [10, 3]. 5 | let y = [9, 1]. 6 | let d = (math @sqrt(math @pow(x@0-y@0,2) + math @pow(x@1-y@1,2))). 7 | io @println d. 8 | 9 | assert (d == 2.23606797749979) 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test069.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let array = [10, 14, 0, 15, 17, 20, 30, 35]. 4 | let x = 17. 5 | io @println ((tostring x +" is in the list") 6 | if array @member x 7 | else (tostring x + " is not in the list")). 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test104.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | try 6 | let 1 = 1.0 . 7 | catch Exception(_,v) do 8 | io @println v. 9 | assert(v is "pattern match failed: nodes 'real' and 'integer' are not the same"). 10 | end 11 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test141.ast: -------------------------------------------------------------------------------- 1 | let Pair = pattern (x,y). 2 | 3 | try 4 | let *Pair bind [x as a, y] = (1,2). -- bindings of the variables x and y are now visible again 5 | catch Exception(errortype,s) do 6 | assert(s is ".*binding term lists.*"). 7 | end 8 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/cls.ast: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env asteroid 2 | load system io. 3 | load system util. 4 | load system random. 5 | 6 | util @cls(). 7 | 8 | for i in 1 to 3 do 9 | io @println ("Random value " + tostring(random@random())). 10 | util @sleep 0.5 11 | end 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test018.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | structure A with 4 | data a. 5 | data b. 6 | end 7 | 8 | let a = A(1,2). 9 | let (v:%A) if v @a + 1 > 1 = a. 10 | let A(x:%string,_) = A("hello",2). 11 | io @println v. 12 | io @println x. 13 | -------------------------------------------------------------------------------- /start: -------------------------------------------------------------------------------- 1 | # bash script to init environment on replit.com 2 | pip3 install asteroid-lang 3 | clear 4 | echo "Welcome to Asteroid!" 5 | echo " " 6 | asteroid 7 | echo " " 8 | echo "Example programs are available in folder:" 9 | echo " " 10 | echo " asteroid/programs" 11 | echo " " 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test027.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let n = 10. -- compute the 10th Fib number 4 | 5 | let (f_1,f_2) = (1,1). 6 | for i in 3 to n do 7 | let (f_1,f_2) = (f_1+f_2,f_1). 8 | end 9 | 10 | io @println f_1. 11 | assert (f_1 == 55) 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test139.ast: -------------------------------------------------------------------------------- 1 | -- test binding terms 2 | let Pair = pattern %[(x,y)]%. 3 | 4 | function add with ( *Pair bind [x as a, y as b], 5 | *Pair bind [x as c, y as d] ) do 6 | return (a+c, b+d). 7 | end 8 | 9 | assert(add((1,2),(3,4))== (4,6)). -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test046.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | let a = [10,20,30]. 6 | let x = a @(1). 7 | let y = a @[1]. 8 | 9 | io @println x. -- prints out a scalar 10 | io @println y. -- prints out a list 11 | assert((x is 20) and (y is [20])). 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test126.ast: -------------------------------------------------------------------------------- 1 | -- test whether or not variable and type names are 2 | -- visible in the current environment 3 | 4 | assert(isdefined("x") == false). 5 | 6 | structure A with 7 | data a. 8 | data b. 9 | end 10 | 11 | assert (isdefined("A")). 12 | 13 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test153.ast: -------------------------------------------------------------------------------- 1 | let p = (1,2). 2 | match p 3 | with (x,y) if x > y do 4 | let x = "GT". 5 | with (x,y) if x < y do 6 | let x = "LT". 7 | with _ do 8 | throw Error("not a valid tuple"). 9 | end 10 | assert(x =="LT"). 11 | 12 | 13 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test062.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system vector. 3 | 4 | let a = [10 to 20]. 5 | let b = [30 to 40]. 6 | let c = vector @op((lambda with (x,y) do return x+y),a,b). 7 | io @println c. 8 | 9 | assert(c == [40,42,44,46,48,50,52,54,56,58,60]). 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/prgm-003.ast: -------------------------------------------------------------------------------- 1 | 2 | let tuple_list = [ 3 | (0,"duck"), 4 | (1,"chicken"), 5 | (2,"turkey") 6 | ]. 7 | 8 | for (1,bird) in tuple_list do 9 | assert(bird is "chicken"). 10 | end 11 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test071.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | function ident 6 | with n do 7 | return n 8 | end 9 | 10 | let y = ident(ident (0)). 11 | let x = ident ident 0. 12 | 13 | io @println (x,y). 14 | assert ((x,y) is (0,0)). 15 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test121.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let p = (pattern with (x, y)). 4 | 5 | function foo 6 | with *p do 7 | io @println("matched"). 8 | assert(true). 9 | with _ do 10 | io @println("did not match"). 11 | assert(false). 12 | end 13 | 14 | foo(1, 2) -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test032.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | -- not equal under the default tolerance of 1E-09 5 | assert (not math @isclose(2.0,2.00001)). 6 | 7 | -- equal under the user defined tolerance of 0.0001 8 | assert (math @isclose(2.0,2.00001,0.0001)). 9 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test078.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | for (x,y) in [(1,1), (2,2), (3,3)] do 5 | io @println (x,y). 6 | end 7 | 8 | -- use unification as a filter 9 | for (2,y) in [(1,11), (1,12), (1,13), (2,21), (2,22), (2,23)] do 10 | io @println y. 11 | end 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test095.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | -- -1 is a value 4 | try 5 | let -1 = -1. 6 | catch e do 7 | io @println e. 8 | end 9 | 10 | -- not true is a computation 11 | try 12 | let not true = not true. 13 | catch e do 14 | io @println e. 15 | end 16 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test042.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | load system vector. 4 | 5 | let x = [10, 3]. 6 | let y = [9, 1]. 7 | let a = vector @sub(x,y). 8 | let d = math @sqrt(vector @dot(a,a)). 9 | io @println d. 10 | 11 | assert (d == 2.23606797749979) 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test040.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system random. 3 | 4 | let hist = [0 to 9] @map(lambda with _ do return 0). 5 | 6 | for _ in range(10000) do 7 | let ix = random @randint(0,9). 8 | let hist @ix = hist @ix +1 9 | end 10 | 11 | io @println hist. 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test075.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system set. 3 | 4 | function unique with lst:%list do 5 | return set @toset lst @sort(). 6 | end 7 | 8 | let a = unique([2, 3, 7, 4, 5, 5, 6, 2, 10, 7]). 9 | 10 | io @println a. 11 | 12 | assert (a == [2,3,4,5,6,7,10]) -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test049.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | let (f,g) = (1,2). 5 | 6 | function foobar 7 | with none do 8 | global f, g. 9 | let f = 2. 10 | end 11 | 12 | foobar(none). 13 | io @println (f,g). 14 | 15 | assert((f,g) is (2,2)). 16 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-034.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | structure Person with 4 | data name. 5 | data age. 6 | end 7 | 8 | let scarlett = Person("Scarlett",28). 9 | -- access the name field of the structure instance 10 | io @println (scarlett@name). 11 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test050.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let number = 139487854. 4 | 5 | 6 | let s = tostring number @explode() 7 | @map tointeger 8 | @reduce (lambda with (x,y) do x+y). 9 | io @println s. 10 | 11 | assert (s == 49). 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test041.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | structure A with 5 | data x. 6 | function dump with none do io @println (this@x) end . 7 | end 8 | 9 | let obj = A(2). 10 | obj @dump(). 11 | 12 | let A(x) = obj. 13 | io @println x. 14 | 15 | assert(x is 2). 16 | 17 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test135.ast: -------------------------------------------------------------------------------- 1 | -- pattern matching does not work with undeclared types 2 | 3 | structure A with 4 | data a. 5 | data b. 6 | end 7 | 8 | try 9 | let B(x,y) = A(1,2). 10 | catch e:Exception(t,m) do 11 | assert(m is "illegal pattern, 'B' is not a type"). 12 | end 13 | 14 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-016.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let b = ("a","b","c"). -- build a tuple 4 | 5 | try 6 | let b@1 = "z". -- attempt to modify an element in the tuple 7 | catch Exception (kind,message) do 8 | io @println (kind+": "+message). 9 | end. 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/prgm-008.ast: -------------------------------------------------------------------------------- 1 | 2 | match (1,2) 3 | with (x,y) if x > y do 4 | let x = "GT". 5 | with (x,y) if x < y do 6 | let x = "LT". 7 | with _ do 8 | throw Error("not a valid tuple"). 9 | end 10 | assert(x == "LT"). 11 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-029.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let list = [1,2,3]. 4 | 5 | repeat do 6 | let [head|tail] = list. -- pattern match with head/tail operator 7 | io @println head. 8 | let list = tail. 9 | until list is []. -- pattern match with is predicate 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test071.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | load system util. 4 | 5 | let mod = math @mod. 6 | 7 | let array = [2, 4, 18, 9, 16, 7, 10]. 8 | let odd = array @reduce (lambda with (acc,i) do i if isnone acc and mod(i,2) =/= 0 else acc,none). 9 | io @println odd. 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test012.ast: -------------------------------------------------------------------------------- 1 | 2 | -- Palindrome list filter 3 | load system io. 4 | 5 | function filter 6 | with (x:%string) if x @explode() == x @explode() @reverse() do 7 | return true. 8 | with _ do 9 | return false. 10 | end 11 | 12 | io @print (filter "radar"). 13 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test051.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | let 2 = 1 + 1. -- (*@\label{patternmatching-models:let1a}@*) 5 | 6 | try 7 | let 1 + 1 = 1 + 1. -- throws an exception (*@\label{patternmatching-models:let3}@*) 8 | catch _ do 9 | io @println "pattern match failed". 10 | end 11 | 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test053.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | let p = -1. 5 | let w = p - 1. 6 | let q = [-1]. 7 | io @println (p,w,q). 8 | 9 | -- NOTE: workaround, pattern matching does not work on negative constants. 10 | assert(p == -1). 11 | assert(w == -2). 12 | assert(q == [-1]). 13 | 14 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test074.ast: -------------------------------------------------------------------------------- 1 | 2 | load "util". 3 | load system io. 4 | 5 | structure MyError with 6 | data e. 7 | end 8 | 9 | try 10 | 11 | throw Error "--- error ---". 12 | 13 | catch Error(msg) do 14 | io @println msg. 15 | assert (msg == "--- error ---") 16 | end 17 | 18 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test118.ast: -------------------------------------------------------------------------------- 1 | 2 | -- regression tests for list pop member function 3 | let x = [1,2,3,4,5,6]. 4 | let y = x@pop(0). 5 | assert(y == 1). 6 | 7 | let x = [1,2,3,4,5,6]. 8 | let y = x@pop(1). 9 | assert(y == 2). 10 | 11 | let x = [1,2,3,4,5,6]. 12 | let y = x@pop(). 13 | assert(y == 6). 14 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-023.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system util 3 | 4 | let indexes = ["first","second","third"]. 5 | let birds = ["turkey","duck","chicken"]. 6 | 7 | for (ix,bird) in util @zip (indexes,birds) do 8 | io @println ("the "+ix+" bird is a "+bird). 9 | end 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test117.ast: -------------------------------------------------------------------------------- 1 | -- head and tail operator tests 2 | 3 | let l1 = [1]. 4 | assert(hd(l1) == 1). 5 | assert(tl(l1) == []). 6 | 7 | 8 | let l1 = [1,2]. 9 | assert(hd(l1) == 1). 10 | assert(tl(l1) == [2]). 11 | 12 | 13 | let l1 = [1,2,3]. 14 | assert(hd(l1) == 1). 15 | assert(tl(l1) == [2,3]). 16 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/prgm-015.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | if (1,2) is (x,y) do 4 | io @println "success". 5 | assert(isdefined "x"). 6 | assert(isdefined "y"). 7 | else 8 | io @println "not matched". 9 | assert(not isdefined "x"). 10 | assert(not isdefined "y"). 11 | end 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test073.ast: -------------------------------------------------------------------------------- 1 | 2 | -- show that the value constructed by head-tail is a list 3 | let [1,2,3] = [1 | [2,3]]. 4 | 5 | -- show that a list can be decomposed with head-tail 6 | let [1 | [2,3]] = [1,2,3]. 7 | 8 | -- show that we can nest head-tail operators 9 | let [1,2,3] = [1 | [2 | [3 | []]]]. 10 | 11 | -------------------------------------------------------------------------------- /asteroid/version.py: -------------------------------------------------------------------------------- 1 | ########################################################################################## 2 | # 3 | # Asteroid Version 4 | # 5 | # (c) University of Rhode Island 6 | ########################################################################################## 7 | 8 | VERSION = "2.0.2" 9 | MAD_VERSION = "0.0.2" 10 | 11 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test072.ast: -------------------------------------------------------------------------------- 1 | 2 | -- Factorial 3 | 4 | load system io. 5 | load system util. 6 | 7 | function fact 8 | with 0 do 9 | return 1 10 | with n do 11 | return n * fact (n-1). 12 | end 13 | 14 | io @println ("The factorial of 3 is: " + tostring (fact 3)). 15 | assert (fact(3) == 6). 16 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test120.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let x = +2. 4 | assert(x == 2). 5 | 6 | let y = +3.0. 7 | assert(y == 3.0). 8 | 9 | let z = toreal(+x) + +y - toreal(+x) * + toreal(-x). 10 | assert(z == 9.0). 11 | 12 | assert(+12 == 12). 13 | assert(+32.3 == 32.3). 14 | assert(+-0.45 == -+0.45). 15 | assert(+(+9 - x) == +7). -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-017a.ast: -------------------------------------------------------------------------------- 1 | -- define a structure of type A 2 | structure A with 3 | data a. 4 | data b. 5 | end 6 | 7 | let obj = A(1,2). -- default constructor, a<-1, b<-2 8 | assert (obj@a == 1). -- access first data member 9 | assert (obj@b == 2). -- access second data member 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test070.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let array = [10, 14, 0, 15, 17, 20, 30, 35]. 4 | let x = 17. 5 | 6 | if array @reduce(lambda with (acc,i) do true if i==x else acc,false) do 7 | io @println (tostring x + " is in the list"). 8 | else 9 | io @println (tostring x + " is not in the list"). 10 | end 11 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/prgm-010.ast: -------------------------------------------------------------------------------- 1 | -- define a structure of type A 2 | structure A with 3 | data a. 4 | data b. 5 | end 6 | 7 | let obj = A(1,2). -- call default constructor 8 | assert( obj@a == 1 ). -- access first data member 9 | assert( obj@b == 2 ). -- access second data member 10 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test137.ast: -------------------------------------------------------------------------------- 1 | -- test expressions computing return values 2 | 3 | load system io. 4 | 5 | function f1 with () do 6 | 3+3. 7 | io @println "hello". 8 | 5. 9 | end 10 | 11 | function f2 with () do 12 | 3+3. 13 | io @println "hello". 14 | end 15 | 16 | assert(f1() is 5). 17 | assert(f2() is none). 18 | 19 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test081.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | let x = 42. 5 | 6 | if x < 0 do 7 | let x = 0. 8 | io @println("Negative changed to zero"). 9 | 10 | elif x == 0 do 11 | io @println("Zero"). 12 | 13 | elif x == 1 do 14 | io @println("Single"). 15 | 16 | else do 17 | io @println("More"). 18 | 19 | end 20 | 21 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-018.ast: -------------------------------------------------------------------------------- 1 | structure Person with 2 | data name. 3 | data age. 4 | data profession. 5 | end 6 | 7 | let joe = Person("Joe", 32, "Cook"). -- construct an object 8 | let Person(n,a,p) = joe. -- pattern match object 9 | 10 | assert (n=="Joe" and a==32 and p=="Cook"). 11 | 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/stream.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system stream. 3 | 4 | let s = stream @stream([1,2,3,4]). 5 | 6 | while not s @eof() do 7 | io @println(s @get()). 8 | end 9 | s @rewind(). 10 | 11 | s @map(lambda with x do return 2*x). 12 | 13 | while not s @eof() do 14 | io @println(s @get()). 15 | end 16 | s @rewind(). 17 | 18 | io @println s. 19 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test103.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | -- reverse the list 6 | let a = [1,2,3]. 7 | let a = a@[2,1,0]. 8 | io @println a. 9 | assert (a is [3,2,1]). 10 | 11 | -- access multidim array 12 | let b = [[1,2,3], 13 | [4,5,6], 14 | [7,8,9]]. 15 | let e = b@1@1. 16 | io @println e. 17 | assert (e is 5). 18 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test081.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | function format with v do 4 | let maxlen = 3. 5 | let vstr = tostring v. 6 | return [1 to maxlen-len(vstr)] @map(lambda with _ do " ") @join("") + vstr. 7 | end 8 | 9 | for i in 1 to 10 do 10 | io @println ([1 to 10] @map(lambda with x do format(i*x)) @join(" ")). 11 | end 12 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test088.ast: -------------------------------------------------------------------------------- 1 | 2 | -- TODO: should this work similar to Rust? 3 | load system io. 4 | load "util". 5 | try 6 | let [1 to 3] = [1,2,3]. 7 | catch Exception(_, v) do 8 | io @println v. 9 | assert (v is "pattern match failed: pattern of type 'to-list' not allowed in pattern matching"). 10 | end 11 | 12 | let [1,2,3] = [1 to 3]. 13 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test063.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system vector. 3 | load system random. 4 | 5 | random @seed(42). 6 | 7 | let a = [1 to 10] @shuffle(). 8 | let b = [1 to 10] @shuffle(). 9 | let c = vector @op((lambda with (x,y) do return x 0 do 7 | n * factorial (n-1). 8 | with (n:%integer) if n < 0 do 9 | throw Error("factorial is not defined for "+n). 10 | end 11 | 12 | io @println ("The factorial of 3 is: " + tostring (factorial 3)). 13 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-011.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | -- build a list of odd values 4 | let a = [1 to 10 step 2]. -- list comprehension 5 | io @println ("list: " + tostring a). 6 | 7 | -- reverse the list using a slice computed as comprehension 8 | let slice = [4 to 0 step -1]. -- list comprehension 9 | let b = a@slice. 10 | io @println ("reversed list: " + tostring b). 11 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-024.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let x = tointeger (io @input "Please enter an integer: "). 4 | 5 | if x < 0 do 6 | let x = 0. 7 | io @println "Negative, changed to zero". 8 | elif x == 0 do 9 | io @println "Zero". 10 | elif x == 1 do 11 | io @println "One". 12 | else do 13 | io @println "Something else". 14 | end 15 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test008.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | function unique with (x,y) do 4 | if not (x @member(y)) do 5 | return x @append(y). 6 | else do 7 | return x. 8 | end 9 | end 10 | 11 | let digits = "332211" @explode() 12 | @sort() 13 | @reduce(unique,[]). 14 | io @println digits. 15 | assert(digits == ["1","2","3"]). 16 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test049.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let val = 42. 4 | 5 | io @println (tobase(val,2)). -- bin 6 | io @println (tobase(val,8)). -- oct 7 | io @println (tobase(val,16)). -- hex 8 | 9 | -- make sure that conversions are correct in both directions 10 | assert (tointeger(tobase(val,2),2) == val). 11 | assert (tointeger(tobase(val,8),8) == val). 12 | assert (tointeger(tobase(val,16),16) == val). 13 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test076.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | function max with lst:%list do 4 | return lst @sort(true) @0. 5 | end 6 | 7 | function min with lst:%list do 8 | return lst @sort() @0. 9 | end 10 | 11 | let v = [7, 6, 12, 3, 4, 10, 2, 5, 15, 6, 7, 8, 9, 3]. 12 | 13 | let a = max v. 14 | let b = min v. 15 | 16 | io @println a. 17 | io @println b. 18 | 19 | assert (a == 15 and b == 2). 20 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test031.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | structure Person with 6 | data name. 7 | data age. 8 | data gender. 9 | end 10 | 11 | let people = [ 12 | Person("George", 32, "M"), 13 | Person("Sophie", 46, "F"), 14 | Person("Oliver", 21, "X") 15 | ]. 16 | 17 | for Person(name:".*p.*",_,_) in people do 18 | io @println name. 19 | end 20 | assert(name is "Sophie") 21 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-031.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | function factorial 4 | with 0 do 5 | return 1 6 | with n if (n is %integer) and (n > 0) do 7 | return n * factorial (n-1). 8 | with n if (n is %integer) and (n < 0) do 9 | throw Error("factorial is not defined for "+n). 10 | end 11 | 12 | io @println ("The factorial of 3 is: " + tostring (factorial 3)). 13 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test066.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | function postfix 6 | with (op, cl, cr) do 7 | return (postfix cl, postfix cr, op) 8 | with (op, c) do 9 | return (postfix c, op) 10 | with (v,) do 11 | return (v,) 12 | end 13 | 14 | io @println (postfix ("+", (1,), (2,))). 15 | assert ((postfix ("+", (1,), (2,))) is ((1,),(2,),"\+")). -- strings in patterns are REs! 16 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test101.ast: -------------------------------------------------------------------------------- 1 | 2 | -- show off our overloaded '+' operator 3 | load system io. 4 | load "util". 5 | 6 | io @println (1 + 1). 7 | assert ((1+1) == 2). 8 | 9 | let s1 = "hello". 10 | let s2 = "world". 11 | let s3 = s1 + " " + s2 +"!". 12 | io @println s3. 13 | assert (s3 == "hello world!"). 14 | 15 | let l1 = [1,2,3]. 16 | let l2 = [4,5,6]. 17 | let l3 = l1 + l2. 18 | io @println l3. 19 | assert (l3 == [1,2,3,4,5,6]). 20 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test124.ast: -------------------------------------------------------------------------------- 1 | -- test pattern match contraints on structure members 2 | structure Foo with 3 | data a. 4 | function __init__ 5 | with x do 6 | let this@a:%integer = x. 7 | end. 8 | end 9 | 10 | let f = Foo(1). 11 | assert( f is Foo(1) ). 12 | 13 | try 14 | let f = Foo("abc"). 15 | assert(false). -- we should never get here 16 | catch _ do 17 | -- expected exception 18 | ... 19 | end 20 | 21 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test074.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | let dt = ["apple", 5 | "pear", 6 | "grape", 7 | "lemon", 8 | "peach", 9 | "apple", 10 | "banana", 11 | "grape", 12 | "pineapple", 13 | "avocado"]. 14 | 15 | let cnt = dt @count("grape"). 16 | io @println cnt. 17 | 18 | assert (cnt == 2). 19 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test036.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | -- fraction a/b 5 | let a = 16. 6 | let b = 280. 7 | 8 | -- reduce fraction 9 | let gcd_val = math @gcd(a,b). 10 | let numerator = a/gcd_val. 11 | let denominator = b/gcd_val. 12 | io @println numerator. 13 | io @println denominator. 14 | 15 | -- show that original and reduced fraction are the same value 16 | assert (a/b == numerator/denominator). 17 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test019.ast: -------------------------------------------------------------------------------- 1 | 2 | -- Factorial 3 | 4 | load system io. 5 | load system util. 6 | 7 | function fact 8 | with 0 do 9 | return 1 10 | with (n:%integer) if n > 0 do 11 | return n * fact (n-1). 12 | with (n:%integer) if n < 0 do 13 | throw Error("factorial is not defined for "+n). 14 | end 15 | 16 | io @println ("The factorial of 3 is: " + tostring (fact (3))). 17 | assert (fact(3) == 6). 18 | 19 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/people.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load "util". 3 | 4 | structure Person with 5 | data name. 6 | data age. 7 | data gender. 8 | end 9 | 10 | -- define a list of persons 11 | let people = [ 12 | Person("George", 32, "M"), 13 | Person("Sophie", 46, "F"), 14 | Person("Oliver", 21, "X") 15 | ]. 16 | 17 | -- print names that contain 'p' 18 | for Person(name:".*p.*",_,_) in people do 19 | io @println name. 20 | end 21 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test019.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let str1 = "cinema". 4 | let str2 = "iceman". 5 | 6 | function normalize with str do 7 | return str @explode() 8 | @sort() 9 | @join(""). 10 | end 11 | 12 | if normalize(str1) == normalize(str2) do 13 | io @println "Anagrams". 14 | else do 15 | io @println "Not anagrams". 16 | end 17 | 18 | assert (normalize(str1) == normalize(str2)). 19 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-032.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | structure Person with 4 | data name. 5 | data age. 6 | end 7 | 8 | -- define a list of persons 9 | let people = [ 10 | Person("George", 32), 11 | Person("Sophie", 46), 12 | Person("Oliver", 21) 13 | ]. 14 | 15 | -- print names that contain 'p' 16 | for Person(name if name is ".*p.*",age) in people do 17 | io @println name. 18 | end 19 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test010.ast: -------------------------------------------------------------------------------- 1 | 2 | -- load modules 3 | load system io. 4 | 5 | structure Family with 6 | data parent1. 7 | 8 | function members 9 | with none do 10 | let Parent(name1) = this @parent1. 11 | let Parent(name1:%string) = this @parent1. 12 | return name1. 13 | end 14 | end -- structure 15 | 16 | structure Parent with 17 | data name. 18 | end 19 | 20 | let family = Family(Parent("Harry")). 21 | io @println (family @members()). 22 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test039.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system util. 3 | 4 | let n = 1234. 5 | let sq = n*n. 6 | let sq_str = tostring sq. 7 | if sq_str @length() < 8 do 8 | let prefix = [1 to 8-sq_str@length()] @map(lambda with _ do return "0") 9 | @join(""). 10 | let sq_str = prefix + sq_str. 11 | end 12 | let rstr = sq_str @[2 to 5]. 13 | let rval = tointeger rstr. 14 | io @println rval. 15 | 16 | assert (rval == 5227) 17 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test026.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let POS_INT = pattern with (x:%integer) if x > 0. 4 | let NEG_INT = pattern with (x:%integer) if x < 0. 5 | 6 | function fact 7 | with 0 do 8 | return 1 9 | with n:*POS_INT do 10 | return n * fact (n-1). 11 | with n:*NEG_INT do 12 | throw Error("factorial is not defined for "+n). 13 | end 14 | 15 | io @println ("The factorial of 3 is: " + tostring(fact 3)). 16 | assert (fact(3) == 6). 17 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test082.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system vector. 3 | load system util. 4 | 5 | let triangle = [[1]]. 6 | let ix = 0. 7 | 8 | for i in 1 to 6 do 9 | let v = triangle @ix. 10 | let v1 = [0] + v. 11 | let v2 = v + [0]. 12 | let new_v = vector @add(v1,v2). 13 | let triangle = triangle + [new_v]. 14 | let ix = ix + 1. 15 | end 16 | 17 | for r in triangle do 18 | io @println (r @map(lambda with v do tostring v) @join(" ")). 19 | end 20 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-036.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | structure Person with 4 | data name. 5 | -- the function is defined in the middle of the data fields 6 | function hello with none do 7 | io @println ("Hello, my name is "+this@name). 8 | end 9 | data age. 10 | end 11 | 12 | -- pattern matching ignores function definitions 13 | let Person(name,age) = Person("Scarlett",28). 14 | io @println (name+" is "+ tostring age+" years old"). 15 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-043a.ast: -------------------------------------------------------------------------------- 1 | -- declare a pattern that matches scalar values 2 | let scalar = pattern %[p if (p is %integer) or (p is %real)]%. 3 | 4 | -- declare a pattern that matches pairs of scalars 5 | let pair = pattern %[(x:*scalar,y:*scalar)]%. 6 | 7 | -- compute the dot product of two pairs of scalars 8 | function dot2d 9 | with (*pair bind [x as a1, y as a2], *pair bind [x as b1, y as b2]) do 10 | a1*b1 + a2*b2 11 | end 12 | 13 | assert(dot2d((1,0),(0,1)) == 0). -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-003.ast: -------------------------------------------------------------------------------- 1 | -- define first-class patterns 2 | let pos_int = pattern (x:%integer) if x > 0. 3 | let neg_int = pattern (x:%integer) if x < 0. 4 | 5 | -- define our factorial function 6 | function fact 7 | with 0 do 8 | return 1 9 | with n:*pos_int do -- use first pattern 10 | return n * fact (n-1). 11 | with n:*neg_int do -- use second pattern 12 | throw Error("undefined for "+n). 13 | end 14 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test033.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | function isprime with x do 5 | if x >= 2 do 6 | for y in range(2,x) do 7 | if math @mod(x,y) == 0 do 8 | return false. 9 | end 10 | end 11 | else do 12 | return false. 13 | end 14 | return true. 15 | end 16 | 17 | io @println (isprime 17). 18 | io @println (isprime 15). 19 | 20 | assert (isprime(17)). 21 | assert (not isprime(15)). 22 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test058.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | function rotate with (l:%list,i:%integer) do 5 | let n = l @length(). 6 | let vix = range n @map(lambda with x do return math @mod(x+i,n)). 7 | return l @vix. 8 | end 9 | 10 | 11 | let a = [1, 3, 5, 7, 9, 11, 13, 15]. 12 | let b = rotate(a,3). 13 | let c = rotate(a,-3). 14 | io @println a. 15 | io @println b. 16 | io @println c. 17 | 18 | assert(b == [7,9,11,13,15,1,3,5] and c == [11,13,15,1,3,5,7,9]). 19 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-038.ast: -------------------------------------------------------------------------------- 1 | function fact 2 | with 0 do 3 | return 1 4 | with (n:%integer) if n > 0 do 5 | return n * fact (n-1). 6 | with (n:%integer) if n < 0 do 7 | throw Error("fact undefined for negative values"). 8 | end 9 | 10 | function sign 11 | with 0 do 12 | return 1 13 | with (n:%integer) if n > 0 do 14 | return 1. 15 | with (n:%integer) if n < 0 do 16 | return -1. 17 | end 18 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test047.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system random. 3 | load system util. 4 | 5 | random @seed(42). 6 | 7 | let n = random @randint(0,10). 8 | let guess = tointeger(io @input("Guess my number between 0 and 10: ")). 9 | while guess =/= n do 10 | if guess < n do 11 | io @println "Too small.". 12 | elif guess > n do 13 | io @println "Too big.". 14 | end 15 | let guess = tointeger(io @input("Try again: ")). 16 | end 17 | io @println "Yes, this is it!". 18 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test114.ast: -------------------------------------------------------------------------------- 1 | -- tests for formatting 2 | 3 | load system io. 4 | load system math. 5 | 6 | 7 | let b = tostring(true,stringformat(10)). 8 | io @println b. 9 | assert(b is " true"). 10 | 11 | let i = tostring(5,stringformat(5)). 12 | io @println i. 13 | assert(i is " 5"). 14 | 15 | let s = tostring("hello there!",stringformat(30)). 16 | io @println s. 17 | assert(s is " hello there!"). 18 | 19 | let r = tostring(math @pi,stringformat(6,3)). 20 | io @println r. 21 | assert(r is " 3.142"). 22 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test020.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let str = "Was it a rat I saw?". 4 | 5 | function clean with str:%string do 6 | return str @tolower() 7 | @replace("[^a-z]",""). 8 | end 9 | 10 | -- only keep lower case letters 11 | let clean_str = clean(str). 12 | 13 | -- check if it is palidromic 14 | if clean_str == clean_str @flip() do 15 | io @println "Palindromic". 16 | else do 17 | io @println "Not palindromic". 18 | end 19 | 20 | assert (clean_str == clean_str @flip()). 21 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test038.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system random. 3 | load system util. 4 | 5 | let randint = random @randint. 6 | 7 | random @seed(42). 8 | 9 | io @println (random @random()). -- random value in [0.0,1.0) 10 | io @println (randint(0.0,1.0)). -- random value in [0.0,1.0] 11 | io @println (randint(0,1)). -- always 0 or 1 12 | 13 | -- generating a random number in the appropriate interval 14 | let n = 10. 15 | io @println (randint(0.0, toreal n)). 16 | io @println (randint(0,n)). 17 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test113.ast: -------------------------------------------------------------------------------- 1 | -- Bitwise operations regression test 2 | load system bitwise. 3 | 4 | assert(bitwise @band(2,4) == 0). 5 | assert(bitwise @bor(2,4) == 6). 6 | assert(bitwise @bnot(2) == -3). 7 | assert(bitwise @bxor(15,3) == 12). 8 | assert(bitwise @brshift(32,2) == 8). 9 | assert(bitwise @blshift(2,4) == 32). 10 | assert(bitwise @blrotate(242, 1) == 229). 11 | assert(bitwise @brrotate(242, 1) == 121). 12 | assert(bitwise @bsetbit(10,2) == 14). 13 | assert(bitwise @bclearbit(5,1) == 4). 14 | assert(bitwise @bsize(8) == 4). 15 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test132.ast: -------------------------------------------------------------------------------- 1 | structure A with 2 | data a. 3 | data b. 4 | end 5 | 6 | let zp = pattern A(p,q). -- pattern constructed purely structural 7 | let zo = A(pattern p, pattern q). -- pattern constructed as an object 8 | 9 | -- both patterns can match objects 10 | assert(A(1,2) is %[*zp]%). 11 | assert(A(1,2) is %[*zo]%). 12 | 13 | -- two patterns are equivalent if they can match each other, 14 | -- but these patterns are not equivalent: they cannot match each other 15 | assert(zo is %[*zp]%). 16 | assert(not(zp is %[*zo]%)). 17 | 18 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test043.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | let values = [727.7, 1086.5, 1091.0, 1361.3, 1490.5, 1956.1]. 5 | 6 | let avg = values @reduce(lambda with (x,y) do return x+y) / toreal(values @length()). 7 | let diff_sq = values @map(lambda with x do return math @pow(x-avg,2)). 8 | let numerator = diff_sq @reduce(lambda with (x,y) do return x+y). 9 | let denominator = values @length() -1. 10 | let sigma = math @sqrt(numerator/toreal denominator). 11 | io @println sigma. 12 | 13 | assert (sigma == 420.96248961952256) 14 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test043.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | structure Person with 6 | data name. 7 | data age. 8 | data sex. 9 | end 10 | 11 | let people = [ 12 | Person("George", 32, "M"), 13 | Person("Sophie", 46, "F"), 14 | Person("Oliver", 21, "M") 15 | ]. 16 | 17 | let Person(name,age,sex) = people @1. 18 | let output_str = name + " is " + tostring age + " years old and is " + ("male" if sex is "M" else "female") + ".". 19 | io @println output_str. 20 | assert(output_str is "Sophie is 46 years old and is female.") 21 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test034.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | function isprime with x do 5 | if x >= 2 do 6 | for y in range(2,x) do 7 | if math @mod(x,y) == 0 do 8 | return false. 9 | end 10 | end 11 | else do 12 | return false. 13 | end 14 | return true. 15 | end 16 | 17 | let cnt = 0. 18 | for (n if isprime(n)) in 1 to 1000000 do 19 | io @println n. 20 | let cnt = cnt+1. 21 | if cnt == 10 do 22 | break. 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test129.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let E = 10.3e-12. 4 | let e = -3E+1. 5 | let n = E + e - 4.00e01 + 0.1. 6 | io @println(n). 7 | assert(n == -69.8999999999897). 8 | 9 | let s = tostring(n,stringformat(10,10,true)). 10 | io @println(s). 11 | assert(s == "-6.9900000000e+01"). 12 | 13 | let s = tostring(n,stringformat(10,10,false)). 14 | io @println(s). 15 | assert(s == "-69.9000000000"). 16 | 17 | let y = 1. 18 | io @println(gettype(y)). 19 | assert(gettype(y) == "integer"). 20 | 21 | let z = 1.0. 22 | io @println(gettype(z)). 23 | assert(gettype(z) == "real"). -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-039.ast: -------------------------------------------------------------------------------- 1 | let pos_int = pattern (x:%integer) if x > 0. 2 | let neg_int = pattern (x:%integer) if x < 0. 3 | 4 | function fact 5 | with 0 do 6 | return 1 7 | with n:*pos_int do 8 | return n * fact (n-1). 9 | with *neg_int do 10 | throw Error("fact undefined for negative values"). 11 | end 12 | 13 | function sign 14 | with 0 do 15 | return 1 16 | with *pos_int do 17 | return 1. 18 | with *neg_int do 19 | return -1. 20 | end 21 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test042.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load "util". 4 | 5 | structure A with 6 | 7 | data x. 8 | data y. 9 | 10 | function __init__ 11 | with (a,b) do 12 | let this @x = a. 13 | let this @y = b. 14 | with none do 15 | let this @x = 1. 16 | let this @y = 2. 17 | end 18 | end 19 | 20 | let obj1 = A("hello","world"). 21 | io @println obj1. 22 | let obj2 = A(). 23 | io @println obj2. 24 | 25 | assert((obj1 @x is "hello") and (obj1 @y is "world")). 26 | assert((obj2 @x is 1) and (obj2 @y is 2)). 27 | 28 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test046.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | load system random. 4 | 5 | let sqrt = math @sqrt. 6 | let pow = math @pow. 7 | let randint = random @randint. 8 | 9 | random @seed(42). 10 | 11 | let inside = 0. 12 | let n = 10000. 13 | for _ in 1 to n do 14 | let point = (randint(-1.0,1.0),randint(-1.0,1.0)). 15 | if sqrt(pow(point@0,2)+pow(point@1,2)) <= 1.0 do 16 | let inside = inside+1. 17 | end 18 | end 19 | let area = 4.0 * toreal inside / toreal n. 20 | io @println area. 21 | 22 | assert (area == 3.1392). 23 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-033.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system random. 3 | 4 | structure Head with 5 | data val. 6 | end 7 | 8 | structure Tail with 9 | data val. 10 | end 11 | 12 | try 13 | let i = random @random (). 14 | if i >= 0.5 do 15 | throw Head i. 16 | else do 17 | throw Tail i. 18 | end 19 | catch Head v do 20 | io @println ("you win with "+tostring (v,stringformat (4,2))). 21 | catch Tail v do 22 | io @println ("you loose with "+tostring (v,stringformat (4,2))). 23 | end 24 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test143.ast: -------------------------------------------------------------------------------- 1 | -- define a structure of type Person 2 | structure Person with 3 | data name. 4 | data age. 5 | function __init__ with (name:%string,age:%integer) do -- constructor 6 | let this @name = name. 7 | let this @age = age. 8 | end 9 | function __str__ with none do 10 | this@name+" is "+ tostring (this@age) +" years old". 11 | end 12 | end 13 | 14 | let betty = Person("Betty",21). -- call constructor 15 | assert( betty @name == "Betty" ). 16 | assert( betty @age == 21 ). 17 | 18 | assert(tostring betty is "Betty is 21 years old"). 19 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/qsort.ast: -------------------------------------------------------------------------------- 1 | -- the quick sort 2 | -- (c) Lutz Hamel, University of Rhode Island 3 | 4 | load system io. 5 | 6 | function qsort 7 | with [] do 8 | []. 9 | with [a] do 10 | [a]. 11 | with [pivot|rest] do 12 | let less=[]. 13 | let more=[]. 14 | for e in rest do 15 | if e < pivot do 16 | let less = less + [e]. 17 | else 18 | let more = more + [e]. 19 | end 20 | end 21 | qsort less + [pivot] + qsort more. 22 | end 23 | 24 | -- print the sorted list 25 | io @println (qsort [3,2,1,0]) 26 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-030.ast: -------------------------------------------------------------------------------- 1 | structure Node with -- internal tree node with a value 2 | data value. 3 | data left_child. 4 | data right_child. 5 | end 6 | 7 | structure Leaf with -- leaf node with a value 8 | data value. 9 | end 10 | 11 | -- traverse a tree and collect all the values in the tree in a list 12 | function traverse 13 | with Leaf(v) do 14 | return [v]. 15 | with Node(v,l,r) do 16 | return [v] + traverse l + traverse r. 17 | end 18 | 19 | let tree = Node(1,Leaf(2),Leaf(3)). 20 | assert (traverse(tree) == [1,2,3]). -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test060.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load system util. 4 | 5 | structure Person with 6 | data name. 7 | data age. 8 | data sex. 9 | end 10 | 11 | let people = [ 12 | Person("George", 32, "M"), 13 | Person("Sophie", 46, "F"), 14 | Person("Oliver", 21, "M") 15 | ]. 16 | 17 | let n = people @length(). 18 | let sum = 0. 19 | 20 | for Person(_,age,_) in people do 21 | let sum = sum + age. 22 | end 23 | 24 | io @println ("Average Age: " + tostring (sum/n)). 25 | assert ((sum/n) == 33). 26 | 27 | for Person(name,_,"M") in people do 28 | io @println name. 29 | end 30 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test064.ast: -------------------------------------------------------------------------------- 1 | 2 | -- implements Peano addition on terms 3 | load system io. 4 | load "util". 5 | 6 | structure S with 7 | data x. 8 | end 9 | 10 | structure add with 11 | data left. 12 | data right. 13 | end 14 | 15 | function reduce 16 | with add(x,0) do 17 | return reduce(x). 18 | with add(x,S(y)) do 19 | return S(reduce(add(x,y))). 20 | with term do 21 | return term. 22 | end 23 | 24 | io @println(reduce(add(add(add(S(S(0)),S(S(S(0)))),S(0)),S(0)))). 25 | assert(reduce(add(add(add(S(S(0)),S(S(S(0)))),S(0)),S(0))) is S(S(S(S(S(S(S(0)))))))). 26 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test014.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let dna2rna_table = 4 | [ 5 | ("A","U"), 6 | ("C","G"), 7 | ("G","C"), 8 | ("T","A") 9 | ]. 10 | 11 | function dna2rna with x do 12 | for (dna,rna) in dna2rna_table do 13 | if x is *dna do 14 | return rna. 15 | end 16 | end 17 | throw Error("unknown dna char "+x). 18 | end 19 | 20 | let dna_seq = "ACCATCAGTC". 21 | let rna_seq = dna_seq @explode() 22 | @map(dna2rna) 23 | @join(""). 24 | io @println rna_seq. 25 | 26 | assert(rna_seq == "UGGUAGUCAG"). 27 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test147.ast: -------------------------------------------------------------------------------- 1 | load system math. 2 | 3 | let test_cases = [ 4 | (3.14, 3), 5 | (3.508, 4), 6 | (12.99999, 13), 7 | (13.00000, 13), 8 | (13.00001, 13), 9 | (13.49999, 13), 10 | (13.50000, 14), 11 | (13.50001, 14), 12 | (13.99999, 14), 13 | (14.00000, 14), 14 | (14.00001, 14), 15 | (14.49999, 14), 16 | (14.50000, 14), 17 | (12.5, 12), 18 | (13.5, 14), 19 | (14.5, 14), 20 | (15.5, 16), 21 | (0.0, 0), 22 | (-0.0, 0), 23 | (-53.246, -53), 24 | (-53.5, -54) 25 | ]. 26 | 27 | for (input, expected_output) in test_cases do 28 | assert (math @round input == expected_output). 29 | end 30 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/prgm-011.ast: -------------------------------------------------------------------------------- 1 | -- define a structure of type Person 2 | structure Person with 3 | data name. 4 | data age. 5 | function __init__ with (name:%string,age:%integer) do -- constructor 6 | let this@name = name. 7 | let this@age = age. 8 | end 9 | function __str__ with none do 10 | return this @name+" is "+ tostring(this@age) +" years old". 11 | end 12 | end 13 | 14 | let betty = Person("Betty",21). -- call constructor 15 | assert( betty@name == "Betty" ). 16 | assert( betty@age == 21 ). 17 | 18 | assert(tostring betty is "Betty is 21 years old"). 19 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test065.ast: -------------------------------------------------------------------------------- 1 | 2 | -- Quicksort 3 | 4 | load system io. 5 | load "util". 6 | 7 | function qsort 8 | with [] do 9 | return []. 10 | with [a] do 11 | return [a]. 12 | with [pivot|rest] do 13 | let less=[]. 14 | let more=[]. 15 | 16 | for e in rest do 17 | if e < pivot do 18 | let less = less + [e]. 19 | else 20 | let more = more + [e]. 21 | end 22 | end 23 | 24 | return qsort less + [pivot] + qsort more. 25 | end 26 | 27 | io @println (qsort [3,2,1,0]). 28 | assert (qsort [3,2,1,0] == [0,1,2,3]). 29 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: Run Tests 2 | on: 3 | pull_request: 4 | branches: 5 | - 'dev-**' 6 | 7 | push: 8 | branches: 9 | - 'dev-**' 10 | env: 11 | PYTHONPATH: ${{ github.workspace }} 12 | jobs: 13 | run-tests: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - name: Check out repository 17 | uses: actions/checkout@v2 18 | 19 | - name: Setup python 20 | uses: actions/setup-python@v4 21 | with: 22 | python-version: '3.10' 23 | 24 | - name: Run tests 25 | run: | 26 | pip install -r requirements.txt 27 | python ${{ github.workspace }}/asteroid/test-suites/run-tests.py 28 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test018.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let stra = "the quick brown fox jumps over the lazy dog". 4 | let strb = "what does the fox say?". 5 | let common = "". 6 | 7 | for startix in 0 to stra @length()-1 do 8 | for endix in startix to stra @length()-1 do 9 | let s = stra @[startix to endix]. 10 | if strb @index(s) =/= -1 and s @length() > common @length() do 11 | let common = s. 12 | end 13 | end 14 | end 15 | 16 | if common =/= "" do 17 | io @println ("The longest common substring is '"+common+"'."). 18 | else do 19 | io @println ("There are no common substrings."). 20 | end 21 | 22 | assert (common == " fox "). 23 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-037.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | structure Dog with 4 | data name. 5 | data tricks. 6 | function __init__ with (name:%string, tricks:%list) do -- constructor 7 | let this@name = name. 8 | let this@tricks = tricks. 9 | end 10 | end 11 | 12 | let fido = Dog("Fido",["play dead","fetch"]). 13 | let buddy = Dog("Buddy",["sit stay","roll over"]). 14 | let bella = Dog("Bella",["roll over","fetch"]). 15 | 16 | let dogs = [fido,buddy,bella]. 17 | 18 | -- print out all the dogs that know how to fetch 19 | for (Dog(name,tricks) if tostring tricks is ".*fetch.*") in dogs do 20 | io @println (name+" knows how to fetch"). 21 | end 22 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/fact.ast: -------------------------------------------------------------------------------- 1 | -- Factorial 2 | -- An implementation of factorial with properly 3 | -- constrained inputs using pattern matching 4 | -- with type classes and conditional matching 5 | 6 | load system io. 7 | 8 | let POS_INT = pattern %[(x:%integer) if x > 0]%. 9 | let NEG_INT = pattern %[(x:%integer) if x < 0]%. 10 | 11 | function fact 12 | with 0 do 13 | return 1 14 | with n:*POS_INT do 15 | return n * fact (n-1). 16 | with n:*NEG_INT do 17 | throw Error("undefined for "+n). 18 | end 19 | 20 | try 21 | let i = tointeger(io @input "Enter an integer value: "). 22 | io @println ("The factorial of "+ tostring i +" is: " + tostring (fact i)). 23 | catch e do 24 | io @println e. 25 | end 26 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test011.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | function title with w do 4 | let letter_list = w @tolower() 5 | @explode(). 6 | let first_letter = letter_list @0 7 | @toupper(). 8 | if letter_list @length() > 1 do 9 | let title_case = ([first_letter] + letter_list @[1 to letter_list@length()-1]) @join(""). 10 | else 11 | let title_case = first_letter. 12 | end 13 | return title_case. 14 | end 15 | 16 | let str = "once upon a time". 17 | let camel_str = str @split() 18 | @map(title) 19 | @join(""). 20 | io @println camel_str. 21 | assert(camel_str == "OnceUponATime"). 22 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test008.ast-not-valid: -------------------------------------------------------------------------------- 1 | -- this test case is no longer valid, we no longer allow 2 | -- patterns as constructors, except for the simplest 3 | -- cases because patterns have become too complicated 4 | load system io. 5 | 6 | structure Family with 7 | data parent1. 8 | 9 | function members 10 | with none do 11 | let Parent(n1:%string) = this @parent1. 12 | return n1. 13 | end 14 | end -- structure 15 | 16 | let FAMILY = pattern with Family(PARENT1). 17 | 18 | structure Parent with 19 | data name. 20 | end 21 | 22 | let PARENT1 = pattern with Parent(p1). 23 | 24 | 25 | let p1 = "Harry". 26 | let family = eval(FAMILY). 27 | --__raw_print__(family). 28 | io @println (family @members()). 29 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test115.ast: -------------------------------------------------------------------------------- 1 | -- testing the builtin 'len' function 2 | 3 | load system io. 4 | 5 | structure Foo with 6 | data one. 7 | data two. 8 | data three. 9 | function hello 10 | with none do 11 | io @println (tostring(this@one)+ 12 | tostring(this@two)+ 13 | tostring(this@three)). 14 | end 15 | end 16 | 17 | let ll = len [1,2,3]. 18 | assert (ll == 3). 19 | io @println ll. 20 | 21 | let lt = len (1,2,3). 22 | assert (lt == 3). 23 | io @println lt. 24 | 25 | let ls = len "123". 26 | assert (ls == 3). 27 | io @println ls. 28 | 29 | let f = Foo(1,2,3). 30 | f @hello(). 31 | let lf = len f. 32 | assert (lf == 3). 33 | let lFoo = len Foo. 34 | assert (lFoo == 3). 35 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test013.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system util. 3 | load system pick. 4 | load system random. 5 | 6 | random @seed(42). 7 | 8 | -- make up lists of symbols useful for password construction 9 | let int_list = [0 to 9] @map(tostring). 10 | let lc_list = [97 to 122] @map(util @achar). -- lower case characters 11 | let uc_list = [65 to 90] @map(util @achar). --upper case characters 12 | let sp_list = ["!","_","#","$","%","*"]. 13 | -- build the overall pick list of symbols 14 | let pick_list = int_list+lc_list+uc_list+sp_list. 15 | 16 | -- generate the password and print it. 17 | let pwd = pick @pick pick_list @pickitems 15 @join(""). 18 | io @println pwd. 19 | 20 | assert (pwd == "e3zvshdbS43brt#") 21 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-017.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | structure Person with 4 | data name. 5 | data age. 6 | data gender. 7 | end 8 | 9 | -- make a list of persons 10 | let people = [ 11 | -- use default constructors to construct Person objects 12 | Person("George", 32, "man"), 13 | Person("Sophie", 46, "woman"), 14 | Person("Oliver", 21, "man") 15 | ]. 16 | 17 | -- retrieve the second person on the list and use pattern 18 | -- matching on Person objects to extract member values 19 | let Person(name,age,gender) = people@1. 20 | 21 | -- print out the member values 22 | io @println (name + " is " + tostring age + " years old and is a " + gender + "."). 23 | -------------------------------------------------------------------------------- /asteroid/test-suites/ref-programs/format.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | -- if the width specifier is larger than the length of the value 5 | -- then the value will be right justified 6 | let b = tostring(true,stringformat(10)). 7 | io @println b. 8 | 9 | let i = tostring(5,stringformat(5)). 10 | io @println i. 11 | 12 | -- we can format a string by applying tostring to the string 13 | let s = tostring("hello there!",stringformat(30)). 14 | io @println s. 15 | 16 | -- for floating point values: first value is width, second value precision. 17 | -- if precision is missing then value is left justified and zero padded on right. 18 | let r = tostring(math@pi,stringformat(6,3)). 19 | io @println r. 20 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-026.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | function qsort 4 | with [] do -- empty list pattern 5 | return []. 6 | with [a] do -- single element list pattern 7 | return [a]. 8 | with [pivot|rest] do -- separating the list into pivot and rest of list 9 | let less=[]. 10 | let more=[]. 11 | 12 | for e in rest do 13 | if e < pivot do 14 | less @append e. 15 | else 16 | more @append e. 17 | end 18 | end 19 | 20 | return qsort less + [pivot] + qsort more. 21 | end 22 | 23 | -- print the sorted list 24 | io @println (qsort [3,2,1,0]) 25 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-052.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | -- if the width specifier is larger than the length of the value 5 | -- then the value will be right justified 6 | let b = tostring(true,stringformat(10)). 7 | io @println b. 8 | 9 | let i = tostring(5,stringformat(5)). 10 | io @println i. 11 | 12 | -- we can format a string by applying tostring to the string 13 | let s = tostring("hello there!",stringformat(30)). 14 | io @println s. 15 | 16 | -- for floating point values: first value is width, second value precision. 17 | -- if precision is missing then value is left justified and zero padded on right. 18 | let r = tostring(math@pi,stringformat(6,3)). 19 | io @println r. 20 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/largest.ast: -------------------------------------------------------------------------------- 1 | -- example inspired by the code from the "The Rust Programming Language" 2 | -- book from No Starch Press pg. 176 3 | 4 | load system io. 5 | load system util. 6 | 7 | function largest with (list:%list,fcmp:%function) do 8 | let largest = list@0. 9 | for item in list do 10 | if fcmp (item,largest) do 11 | let largest = item. 12 | end 13 | end 14 | largest 15 | end 16 | 17 | let number_list = [34,50,25,100,65]. 18 | let result = largest (number_list,lambda with (x,y) do x > y). 19 | io @println("The largest number is " + tostring result). 20 | 21 | let char_list = ["y","m","a","q"]. 22 | let result = largest (char_list,lambda with (x,y) do util @ascii x > util @ascii y). 23 | io @println("The largest charactor is " + tostring result). 24 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test044.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | -- define common math functions locally so the 5 | -- formulas are easy to read 6 | let cos = math @cos. 7 | let sin = math @sin. 8 | let sqrt = math @sqrt. 9 | let pow = math @pow. 10 | let atan = math @atan. 11 | 12 | function polar_to_cartesian with (r,psi) do 13 | -- return a tuple: (x,y) 14 | return (r*cos(psi),r*sin(psi)). 15 | end 16 | 17 | function cartesian_to_polar with (x,y) do 18 | -- return a tuple: (r,psi) 19 | return (sqrt(pow(x,2)+pow(y,2)),atan(y/x)). 20 | end 21 | 22 | let (r,psi) = cartesian_to_polar(1,2). 23 | let (x,y) = polar_to_cartesian(r,psi). 24 | 25 | io @println (x,y). 26 | 27 | -- show that the recovered coordinates are the same 28 | -- we started with 29 | assert (math @isclose(1,x,0.0001) and math @isclose(2,y,0.0001)). 30 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test119.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let COORDINATE = pattern with (x,y). 4 | let COORDINATE_LIST = pattern with coordinates:%list. 5 | let POINTS = pattern with points:%integer. 6 | let COORDINATES = pattern with ( *COORDINATE_LIST , *POINTS ). 7 | let POINT_ONE = pattern with p1:(x1,y1). 8 | let POINT_TWO = pattern with p2:(x2,y2). 9 | let POINT_THREE = pattern with p3:(x3,y3). 10 | let TWO_POINTS = pattern with ( *POINT_ONE , *POINT_TWO ). 11 | let THREE_POINTS = pattern with ( *POINT_ONE , *POINT_TWO , *POINT_THREE ). 12 | 13 | function foo 14 | with x:%pattern do 15 | io @println("is a pattern"). 16 | with x if not(x is %pattern) do 17 | io @println("not a pattern"). 18 | end 19 | 20 | -- Should match as a pattern 21 | foo(THREE_POINTS). 22 | 23 | -- Should not match as a pattern 24 | foo("Asteroid"). 25 | foo(123). 26 | foo((1,2,3)). 27 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test079.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system hash. 3 | load system sort. 4 | load system random. 5 | 6 | let randint = random @randint. 7 | 8 | random @seed(42). 9 | 10 | -- hash of names with ages 11 | let ht = hash @hash(). 12 | ht @insert("Billie",randint(20,50)). 13 | ht @insert("Joe",randint(20,50)). 14 | ht @insert("Pete",randint(20,50)). 15 | ht @insert("Brandi",randint(20,50)). 16 | 17 | -- export the hash as a list of pairs 18 | let lst = ht @aslist(). 19 | 20 | -- define our order predicate on a 21 | -- list of pairs where the second 22 | -- component holds the order info 23 | function pairs with ((_,x),(_,y)) do 24 | return true if x < y else false. 25 | end 26 | 27 | -- print out the sorted list 28 | io @println (sort @sort(pairs,lst)). 29 | 30 | assert (sort @sort(pairs,lst) == [("Pete",20),("Joe",23),("Billie",40),("Brandi",43)]) 31 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test021.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let str = "Hello, World!". 4 | 5 | function clean with str:%string do 6 | return str @tolower() 7 | @replace("[^a-z]",""). 8 | end 9 | 10 | function palindrome_test with str:%string do 11 | let clean_str = clean(str). 12 | if clean_str == clean_str @flip() do 13 | return true. 14 | else do 15 | return false. 16 | end 17 | end 18 | 19 | -- create the moving window over the string 20 | let longest_palindrome = "". 21 | 22 | for i in 0 to str @length()-2 do 23 | for j in i+1 to str @length()-1 do 24 | let str1 = str @[i to j]. 25 | if palindrome_test(str1) and 26 | str1 @length() > longest_palindrome @length() do 27 | let longest_palindrome = str1. 28 | end 29 | end 30 | end 31 | 32 | io @println longest_palindrome. 33 | -------------------------------------------------------------------------------- /asteroid/test-suites/redundant-pattern-test/test003.ast: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------- 3 | -- patterns_test2.ast 4 | -- a simple program to test for 5 | -- subsumption detection in Asteroid. 6 | -------------------------------------- 7 | -------------------------------------- 8 | load system io. 9 | load "util". 10 | 11 | -- A testing function with multiple patterns 12 | function testFunction 13 | with 1 do --will SUBSUME 1 14 | return 1. 15 | with 2 do 16 | return 2. 17 | with 1 do 18 | return 3. 19 | end. 20 | 21 | try 22 | -- Evaluate the function with a test input. 23 | let x = testFunction( 1 ). 24 | 25 | -- If the function test worked, print the output we got. 26 | io @println( "The value returned is: " + tostring x ). 27 | throw Error("Fail: subsumption not detected") 28 | 29 | catch Exception("RedundantPatternFound",s) do 30 | io @println("PASS."). 31 | end. -------------------------------------------------------------------------------- /asteroid/test-suites/redundant-pattern-test/test012.ast: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------- 3 | -- patterns_test11.ast 4 | -- a simple program to test for 5 | -- subsumption detection in Asteroid. 6 | -------------------------------------- 7 | -------------------------------------- 8 | load system io. 9 | load "util". 10 | 11 | function test_function 12 | with ".*q.*" do 13 | return "Found a p.". 14 | with ".*http.*" do -- RE ".*http.*" will match to all ".*https.*" patterns 15 | return "Found a url.". 16 | with ".*https.*" do --Redundant 17 | return "Found a secure url.". 18 | with ".*z.*" do 19 | return "Found a z.". 20 | end. 21 | 22 | try 23 | 24 | let var = test_function( "test string z" ). 25 | io @println ( var ). 26 | throw Error("Fail: subsumption not detected") 27 | 28 | catch Exception("RedundantPatternFound",s) do 29 | io @println("PASS."). 30 | end. 31 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test053.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | load system util. 4 | load system hash. 5 | 6 | let roman_hash = hash @hash(). 7 | roman_hash @insert(1000,["","M","MM","MMM"]). 8 | roman_hash @insert(100,["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"]). 9 | roman_hash @insert(10,["","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"]). 10 | roman_hash @insert(1,["","I","II","III","IV","V","VI","VII","VIII","IX"]). 11 | 12 | let n = 2018. 13 | let p10 = range(tostring n @length()) @map(lambda with x do math @pow (10,x)) 14 | @reverse(). 15 | let digits = tostring n @explode () 16 | @map tointeger. 17 | let z = util @zip (digits, p10). 18 | io @println z. 19 | let roman = "". 20 | for (d,p) in z do 21 | let roman = roman + roman_hash @get(p) @d. 22 | end 23 | io @println roman. 24 | 25 | assert (roman == "MMXVIII") 26 | -------------------------------------------------------------------------------- /asteroid/test-suites/redundant-pattern-test/test011.ast: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------- 3 | -- patterns_test10.ast 4 | -- a simple program to test for 5 | -- subsumption detection in Asteroid. 6 | -------------------------------------- 7 | -------------------------------------- 8 | load system io. 9 | load "util". 10 | 11 | function test_function 12 | with ".*p.*" do -- RE ".*p.*"will match to all ".*http.*" and ".*https.*" 13 | return "Found a p.". 14 | with ".*http.*" do --Redundant 15 | return "Found a url.". 16 | with ".*https.*" do --Redundant 17 | return "Found a secure url.". 18 | with ".*z.*" do 19 | return "Found a z.". 20 | end. 21 | 22 | try 23 | 24 | let var = test_function( "test string z" ). 25 | io @println ( var ). 26 | throw Error("Fail: subsumption not detected") 27 | 28 | catch Exception("RedundantPatternFound",s) do 29 | io @println("PASS."). 30 | end. 31 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/teams.ast: -------------------------------------------------------------------------------- 1 | -- given a list of names generate 2 | -- and print teams of three 3 | 4 | load system util. 5 | load system io. 6 | load system dataframe. 7 | 8 | -- team participants 9 | let df = dataframe @dataframe(). 10 | df @read_csv("programs/names.csv",true). 11 | let names = df @col(0). 12 | 13 | -- shuffle names 14 | names @shuffle(). 15 | 16 | -- select teams of three 17 | let teams = []. 18 | repeat do 19 | if len(names) < 3 do 20 | let teams = teams + [names]. 21 | break. 22 | elif names is [m1|m2|m3|names] do 23 | let teams = teams + [[m1,m2,m3]]. 24 | end 25 | until names is []. 26 | 27 | -- print teams 28 | for (i,team) in util @zip(1 to len(teams),teams) do 29 | let team_str = "team " + tostring i + (": " if i < 10 else ": "). 30 | repeat do 31 | let [name|team] = team. 32 | let team_str = team_str + name + ("" if team is [] else ", "). 33 | until team is []. 34 | io @println team_str. 35 | end 36 | -------------------------------------------------------------------------------- /asteroid/test-suites/redundant-pattern-test/test001.ast: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------- 3 | -- patterns_test0.ast 4 | -- a simple program to test for 5 | -- subsumption detection in Asteroid. 6 | -------------------------------------- 7 | -------------------------------------- 8 | load system io. 9 | load "util". 10 | 11 | -- A testing function with multiple patterns 12 | function testFunction 13 | with x do --will SUBSUME all other patterns 14 | return 0. 15 | with ( 1, 2, z) do 16 | return 1. 17 | with ( 1, y, z) do 18 | return 2. 19 | with ( x, y, z) do 20 | return 3. 21 | end. 22 | 23 | 24 | try 25 | -- Evaluate the function with a test input. 26 | let x = testFunction( 1 , 2 , 3 ). 27 | io @println( "The value returned is: " + tostring x ). 28 | throw Error("Fail: subsumption not detected") 29 | catch Exception("RedundantPatternFound",s) do 30 | io @println("PASS."). 31 | end. 32 | -------------------------------------------------------------------------------- /asteroid/test-suites/redundant-pattern-test/test005.ast: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------- 3 | -- patterns_test4.ast 4 | -- a simple program to test for 5 | -- subsumption detection in Asteroid. 6 | -------------------------------------- 7 | -------------------------------------- 8 | load system io. 9 | load "util". 10 | 11 | -- A testing function with multiple patterns 12 | function testFunction 13 | with ( x, y, z) do -- *** will SUBSUME ( (x,y), (x,y), (x,y) ) *** 14 | return 1. 15 | with ( (x1,y1), (x2,y2), (x3,y3) ) do 16 | return 2. 17 | end. 18 | 19 | try 20 | -- Evaluate the function with a test input. 21 | let x = testFunction( 1 , 2 , 3 ). 22 | 23 | -- If the function test worked, print the output we got. 24 | io @println( "The value returned is: " + tostring x ). 25 | throw Error("Fail: subsumption not detected") 26 | 27 | catch Exception("RedundantPatternFound",s) do 28 | io @println("PASS."). 29 | end. -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test040.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load system util. 4 | 5 | structure Dog with 6 | 7 | data name. 8 | data tricks. 9 | 10 | function add_trick 11 | with new_trick do 12 | let this @tricks = this @tricks + [new_trick]. 13 | end 14 | 15 | function __init__ 16 | with name do 17 | let this @name = name. 18 | let this @tricks = []. 19 | end 20 | 21 | end 22 | 23 | -- Fido the dog 24 | let fido = Dog("Fido"). 25 | 26 | fido @add_trick("roll over"). 27 | fido @add_trick("play dead"). 28 | 29 | -- Buddy the dog 30 | let buddy = Dog("Buddy"). 31 | 32 | buddy @add_trick("roll over"). 33 | buddy @add_trick("sit stay"). 34 | 35 | -- io @println out the tricks 36 | io @println ("Fido: " + tostring (fido@tricks)). 37 | io @println ("Buddy: " + tostring (buddy@tricks)). 38 | 39 | assert(fido @tricks is ["roll over", "play dead"]). 40 | assert(buddy @tricks is ["roll over", "sit stay"]). 41 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test007.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | 4 | structure Dog with 5 | 6 | data name. 7 | data tricks. 8 | 9 | function add_trick 10 | with new_trick do 11 | let this @tricks = this @tricks + [new_trick]. 12 | end 13 | 14 | function __init__ 15 | with name do 16 | let this @name = name. 17 | let this @tricks = []. 18 | end 19 | 20 | end -- structure 21 | 22 | -- Fido the dog 23 | let fido = Dog("Fido"). 24 | fido @add_trick("roll over"). 25 | fido @add_trick("play dead"). 26 | 27 | -- Buddy the dog 28 | let buddy = Dog("Buddy"). 29 | buddy @add_trick("roll over"). 30 | buddy @add_trick("sit stay"). 31 | 32 | -- Fifi the dog 33 | let fifi = Dog("Fifi"). 34 | fifi @add_trick("sit stay"). 35 | 36 | -- print out all the names of dogs 37 | -- whose first trick is 'roll over'. 38 | let dogs = [fido, buddy, fifi]. 39 | 40 | for Dog(name, ["roll over"|_]) in dogs do 41 | io @println (name + " does roll over"). 42 | end 43 | -------------------------------------------------------------------------------- /asteroid/test-suites/redundant-pattern-test/test002.ast: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------- 3 | -- patterns_test1.ast 4 | -- a simple program to test for 5 | -- subsumption detection in Asteroid. 6 | -------------------------------------- 7 | -------------------------------------- 8 | load system io. 9 | load "util". 10 | 11 | -- A testing function with multiple patterns 12 | function testFunction 13 | with ( 1, value1, value2) do --will SUBSUME ( 1, y, z) 14 | return 1. 15 | with ( 1, y, z) do 16 | return 2. 17 | with ( x, y, z) do 18 | return 3. 19 | end. 20 | 21 | 22 | 23 | try 24 | -- Evaluate the function with a test input. 25 | let x = testFunction( 1 , 2 , 3 ). 26 | 27 | -- If the function test worked, print the output we got. 28 | io @println( "The value returned is: " + tostring x ). 29 | throw Error("Fail: subsumption not detected") 30 | catch Exception("RedundantPatternFound",s) do 31 | io @println("PASS."). 32 | end. 33 | 34 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test035.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | 4 | function isprime with x do 5 | if x >= 2 do 6 | for y in range(2,x) do 7 | if math @mod(x,y) == 0 do 8 | return false. 9 | end 10 | end 11 | else do 12 | return false. 13 | end 14 | return true. 15 | end 16 | 17 | function primes with x do 18 | let lp = []. 19 | for (n if isprime(n)) in 1 to x do 20 | let lp = lp+[n]. 21 | end 22 | return lp. 23 | end 24 | 25 | let n = 165. 26 | let factors = []. 27 | let primes_list = primes(n). 28 | let ix = 0. 29 | 30 | while n > 1 do 31 | let factor = primes_list @ix. 32 | let ix = ix+1. 33 | if math @mod(n,factor) == 0 do 34 | let ix = 0. 35 | let n = n/factor. 36 | let factors = factors+[factor]. 37 | end 38 | end 39 | io @println factors. 40 | 41 | assert (factors == [3,5,11]) 42 | -------------------------------------------------------------------------------- /asteroid/test-suites/redundant-pattern-test/test010.ast: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------- 3 | -- patterns_test9.ast 4 | -- a simple program to test for 5 | -- subsumption detection in Asteroid. 6 | -------------------------------------- 7 | -------------------------------------- 8 | load system io. 9 | load "util". 10 | 11 | -- A testing function with multiple patterns 12 | function testFunction 13 | with true do 14 | return 1. 15 | with false do -- *** will subsume false 16 | return 2. 17 | with false do -- *** duplicate pattern 18 | return 2. 19 | end. 20 | 21 | try 22 | -- Evaluate the function with a test input. 23 | let x = true. 24 | let y = testFunction( x ). 25 | 26 | -- If the function test worked, print the output we got. 27 | io @println( "The value returned is: " + tostring y ). 28 | throw Error("Fail: subsumption not detected") 29 | 30 | catch Exception("RedundantPatternFound",s) do 31 | io @println("PASS."). 32 | end. 33 | -------------------------------------------------------------------------------- /asteroid/test-suites/redundant-pattern-test/test006.ast: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------- 3 | -- patterns_test5.ast 4 | -- a simple program to test for 5 | -- subsumption detection in Asteroid. 6 | -------------------------------------- 7 | -------------------------------------- 8 | load system io. 9 | load "util". 10 | 11 | -- A testing function with multiple patterns 12 | function testFunction 13 | with "name1" do 14 | return 1. 15 | with "name2" do -- *** will SUBSUME "name2" *** 16 | return 2. 17 | with "name2" do -- duplicate pattern 18 | return 2. 19 | with "name4" do 20 | return 2. 21 | end. 22 | 23 | try 24 | -- Evaluate the function with a test input. 25 | let x = testFunction( "name4" ). 26 | 27 | -- If the function test worked, print the output we got. 28 | io @println( "The value returned is: " + tostring x ). 29 | throw Error("Fail: subsumption not detected") 30 | 31 | catch Exception("RedundantPatternFound",s) do 32 | io @println("PASS."). 33 | end. -------------------------------------------------------------------------------- /asteroid/test-suites/redundant-pattern-test/test008.ast: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------- 3 | -- patterns_test7.ast 4 | -- a simple program to test for 5 | -- subsumption detection in Asteroid. 6 | -------------------------------------- 7 | -------------------------------------- 8 | load system io. 9 | load "util". 10 | 11 | -- A testing function with multiple patterns 12 | function testFunction 13 | with 1.1 do -- *** will subsume 1.1 14 | return 1. 15 | with 1.2 do 16 | return 2. 17 | with 1.3 do 18 | return 3. 19 | with 1.1 do -- *** duplicate pattern 20 | return 4. 21 | end. 22 | 23 | 24 | try 25 | -- Evaluate the function with a test input. 26 | let x = 1.1. 27 | let y = testFunction( x ). 28 | 29 | -- If the function test worked, print the output we got. 30 | io @println( "The value returned is: " + tostring y ). 31 | throw Error("Fail: subsumption not detected") 32 | 33 | catch Exception("RedundantPatternFound",s) do 34 | io @println("PASS."). 35 | end. 36 | -------------------------------------------------------------------------------- /asteroid/test-suites/redundant-pattern-test/test009.ast: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------- 3 | -- patterns_test8.ast 4 | -- a simple program to test for 5 | -- subsumption detection in Asteroid. 6 | -------------------------------------- 7 | -------------------------------------- 8 | load system io. 9 | load "util". 10 | 11 | -- A testing function with multiple patterns 12 | function testFunction 13 | with 1.10 do -- *** will subsume 1.10000... 14 | return 1. 15 | with 1.2 do 16 | return 2. 17 | with 1.3 do 18 | return 3. 19 | with 1.10000 do -- *** duplicate pattern 1.10 20 | return 4. 21 | end. 22 | 23 | try 24 | -- Evaluate the function with a test input. 25 | let x = 1.1. 26 | let y = testFunction( x ). 27 | 28 | -- If the function test worked, print the output we got. 29 | io @println( "The value returned is: " + tostring y ). 30 | throw Error("Fail: subsumption not detected") 31 | 32 | catch Exception("RedundantPatternFound",s) do 33 | io @println("PASS."). 34 | end. -------------------------------------------------------------------------------- /asteroid/test-suites/programs/dogs.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | structure Dog with 4 | data name. 5 | data tricks. -- list of trick strings 6 | end 7 | 8 | let fido = Dog ("Fido",["play dead","fetch"]). 9 | let buddy = Dog ("Buddy",["sit stay","roll over"]). 10 | 11 | -- print out all the dogs that know how to fetch 12 | -- we do this with first class pattern defines a dog 13 | -- that knows how to fetch, we use regular expression 14 | -- matching on the trick list 15 | let fetcher = pattern %[(Dog(name,tricks) 16 | if tostring tricks is ".*fetch.*")]%. 17 | 18 | -- one way to do this 19 | for (Dog(name,tricks) if Dog(name,tricks) is *fetcher) in [fido,buddy] do 20 | io @println (name+" knows how to fetch"). 21 | end 22 | 23 | -- another way to do this 24 | for ((d:Dog(name,tricks)) if d is *fetcher) in [fido,buddy] do 25 | io @println (name+" knows how to fetch"). 26 | end 27 | 28 | -- yet another way of doing this 29 | for (*fetcher bind name) in [fido,buddy] do 30 | io @println (name+" knows how to fetch"). 31 | end 32 | 33 | 34 | -------------------------------------------------------------------------------- /asteroid/test-suites/redundant-pattern-test/test004.ast: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------- 3 | -- patterns_test3.ast 4 | -- a simple program to test for 5 | -- subsumption detection in Asteroid. 6 | -------------------------------------- 7 | -------------------------------------- 8 | load system io. 9 | load "util". 10 | 11 | -- A testing function with multiple patterns 12 | function testFunction 13 | with ( 1, 2, 3) do 14 | return 1. 15 | with ( 1, 2, ( x1, "test_string" ) ) do --will SUBSUME ( 1, 2, ( var,"test_string")) 16 | return 2. 17 | with ( x, y, z) do 18 | return 3. 19 | with ( 1, 2, ( var, "test_string" ) ) do 20 | return 3 21 | end. 22 | 23 | 24 | try 25 | -- Evaluate the function with a test input. 26 | let x = testFunction( 1 , 2 , 3 ). 27 | 28 | -- If the function test worked, print the output we got. 29 | io @println( "The value returned is: " + tostring x ). 30 | throw Error("Fail: subsumption not detected") 31 | 32 | catch Exception("RedundantPatternFound",s) do 33 | io @println("PASS."). 34 | end. 35 | -------------------------------------------------------------------------------- /asteroid/test-suites/redundant-pattern-test/test027.ast: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------- 3 | -- patterns_test26.ast 4 | -- a simple program to test for 5 | -- subsumption detection in Asteroid. 6 | -------------------------------------- 7 | -------------------------------------- 8 | -- implements Peano addition on terms 9 | load system io. 10 | load "util". 11 | 12 | structure S with 13 | data x. 14 | end 15 | 16 | structure add with 17 | data left. 18 | data right. 19 | end 20 | 21 | function reduce 22 | with add(x,y) do -- Will subsume all matches for add(x,S(y)) 23 | return 1. 24 | with add(x,0) do 25 | return reduce(x). 26 | with add(x,S(y)) do -- The redundant pattern 27 | return S(reduce(add(x,y))). 28 | with term do 29 | return term. 30 | end 31 | 32 | try 33 | io @println(reduce(add(add(add(S(S(0)),S(S(S(0)))),S(0)),S(0)))). 34 | throw Error("Fail: subsumption not detected") 35 | 36 | catch Exception("RedundantPatternFound",s) do 37 | io @println("PASS."). 38 | end. 39 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test080.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system hash. 3 | load system sort. 4 | 5 | let fruit_hash = hash @hash(). 6 | fruit_hash @insert("apple","red"). 7 | fruit_hash @insert("avocado","green"). 8 | fruit_hash @insert("banana","yellow"). 9 | fruit_hash @insert("grapefruit","orange"). 10 | fruit_hash @insert("grapes","green"). 11 | fruit_hash @insert("kiwi","green"). 12 | fruit_hash @insert("lemon","yellow"). 13 | fruit_hash @insert("orange","orange"). 14 | fruit_hash @insert("pear","green"). 15 | fruit_hash @insert("plum","purple"). 16 | 17 | let fruit_lst = fruit_hash @aslist(). 18 | 19 | let color_hash = hash @hash(). 20 | for (_,color) in fruit_lst do 21 | if color_hash @get(color) is none do 22 | color_hash @insert(color,1). 23 | else 24 | color_hash @insert(color, color_hash @get(color) +1). 25 | end 26 | end 27 | let color_lst = color_hash @aslist(). 28 | 29 | function pairs with ((_,x),(_,y)) do 30 | return true if x < y else false. 31 | end 32 | 33 | io @println (sort @sort(pairs,color_lst)). 34 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/lexer.ast: -------------------------------------------------------------------------------- 1 | -- a convenient interface to a token stream produced by the 2 | -- tokenize function. 3 | -- Lutz Hamel, (c) University of Rhode Island 4 | 5 | load tokenizer. 6 | 7 | structure Lexer with 8 | data token_stream. 9 | 10 | function __init__ with input:%string do 11 | let this@token_stream = tokenizer @tokenize input. 12 | end 13 | 14 | function get 15 | with none do 16 | return this @token_stream @get(). 17 | end 18 | 19 | function peek 20 | with none do 21 | return this @token_stream @peek(). 22 | end 23 | 24 | function eof 25 | with none do 26 | return this @token_stream @eof(). 27 | end 28 | 29 | function token_match 30 | with token_type do 31 | let token = this @token_stream @peek(). 32 | if token @type == token_type do 33 | this @token_stream @get(). 34 | else do 35 | throw Error("expected token "+token_type+" got "+token @type). 36 | end 37 | end 38 | 39 | end -- structure 40 | 41 | if toplevel() do 42 | load system io. 43 | let lex = Lexer("1+1"). 44 | io @println (lex @token_stream). 45 | end 46 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021- University of Rhode Island 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.readthedocs.yaml: -------------------------------------------------------------------------------- 1 | # Read the Docs configuration file for Sphinx projects 2 | # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details 3 | 4 | # Required 5 | version: 2 6 | 7 | # Set the OS, Python version and other tools you might need 8 | build: 9 | os: ubuntu-22.04 10 | tools: 11 | python: "3.11" 12 | # You can also specify other tool versions: 13 | # nodejs: "20" 14 | # rust: "1.70" 15 | # golang: "1.20" 16 | 17 | # Build documentation in the "docs/" directory with Sphinx 18 | sphinx: 19 | configuration: docs/conf.py 20 | # You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs 21 | # builder: "dirhtml" 22 | # Fail on all warnings to avoid broken references 23 | # fail_on_warning: true 24 | 25 | # Optionally build your docs in additional formats such as PDF and ePub 26 | # formats: 27 | # - pdf 28 | # - epub 29 | 30 | # Optional but recommended, declare the Python requirements required 31 | # to build your documentation 32 | # See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html 33 | python: 34 | install: 35 | - requirements: docs/requirements.txt -------------------------------------------------------------------------------- /asteroid/test-suites/redundant-pattern-test/test028.ast: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------- 3 | -- patterns_test27.ast 4 | -- a simple program to test for 5 | -- subsumption detection in Asteroid. 6 | -------------------------------------- 7 | -------------------------------------- 8 | load system io. 9 | load "util". 10 | 11 | structure Dog 12 | with 13 | data name. 14 | data tricks. 15 | 16 | function __init__ 17 | with (name, tricks) do -- Will subsume all matches for (name, []) 18 | let this@name = name. 19 | let this@tricks = tricks. 20 | with (name, []) do -- Redundant Constructor 21 | let this@name = name. 22 | let this@tricks = []. 23 | with name do 24 | let this@name = name. 25 | let this@tricks = []. 26 | end 27 | 28 | function add_trick 29 | with new_trick do 30 | let this@tricks = this@tricks + [new_trick]. 31 | end 32 | end 33 | 34 | try 35 | 36 | -- Fido the dog 37 | let fido = Dog("Fido"). 38 | throw Error("Fail: subsumption not detected") 39 | 40 | catch Exception("RedundantPatternFound",s) do 41 | io @println("PASS."). 42 | end. 43 | -------------------------------------------------------------------------------- /asteroid/modules/sort.ast: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------ 2 | -- sort.ast 3 | -- 4 | -- defines a parameterized sort function over a list. it uses 5 | -- a user defined order predicate on elements of the list to 6 | -- perform the sort. the underlying sort algorithm is the 7 | -- Quicksort. 8 | -- 9 | -- Example: 10 | -- sort((lambda with (x,y) do return true if x number do 30 | io @println "Your guess is too high." 31 | elif guess == number do 32 | break. 33 | end 34 | end 35 | 36 | if guess == number do 37 | io @println ("Good job, " + name + "! You guessed my number in " + tostring guesses_made + " guesses!"). 38 | else do 39 | io @println ("Nope. The number I was thinking of was " + tostring number). 40 | end 41 | 42 | io @println "". 43 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/boom.ast: -------------------------------------------------------------------------------- 1 | -- this is the example program for multi-dispatch in wikipedia 2 | -- https://en.wikipedia.org/wiki/Multiple_dispatch 3 | 4 | load system io. 5 | 6 | structure Asteroid with 7 | data size. 8 | end 9 | 10 | structure Spaceship with 11 | data size. 12 | end 13 | 14 | -- instead of using a base class we use a first-class pattern to 15 | -- express that both asteroids and space ships are space objects. 16 | -- SpaceObject is a pure constraint pattern! 17 | let SpaceObject = pattern with %[x if (x is %Asteroid) or (x is %Spaceship)]%. 18 | 19 | function collide_with 20 | with (a:%Asteroid, b:%Spaceship) do 21 | return "a/s". 22 | with (a:%Spaceship, b:%Asteroid) do 23 | return "s/a". 24 | with (a:%Spaceship, b:%Spaceship) do 25 | return "s/s". 26 | with (a:%Asteroid, b:%Asteroid) do 27 | return "a/a". 28 | end 29 | 30 | -- here we use the first-class pattern SpaceObject as a 31 | -- constraint on the function parameters. 32 | function collide with (x:*SpaceObject, y:*SpaceObject) do 33 | return "Big boom!" if (x@size > 100 and y@size > 100) else collide_with(x, y). 34 | end 35 | 36 | assert (collide(Asteroid(101), Spaceship(300))=="Big boom!"). 37 | assert (collide(Asteroid(10), Spaceship(10))=="a/s"). 38 | assert (collide(Spaceship(101), Spaceship(10))=="s/s"). 39 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # when cutting releases make sure that PYTHONPATH points 2 | # to the root directory of the github repo 3 | 4 | from setuptools import setup, find_packages 5 | from asteroid.version import VERSION 6 | 7 | with open("README.md", "r") as fh: 8 | long_description = fh.read() 9 | 10 | setup( 11 | name="asteroid-lang", 12 | version=VERSION, 13 | author="University of Rhode Island", 14 | author_email="lutzhamel@uri.edu", 15 | description="A modern, multi-paradigm programming language.", 16 | long_description=long_description, 17 | long_description_content_type="text/markdown", 18 | url="https://asteroid-lang.org", 19 | project_urls={ 20 | "Documentation": "https://asteroid-lang.readthedocs.io", 21 | "Bug Tracker": "https://github.com/asteroid-lang/asteroid/issues", 22 | }, 23 | packages=find_packages(), 24 | package_data={"asteroid": ["modules/*"]}, 25 | install_requires=[ 26 | "pandas", 27 | "numpy", 28 | "matplotlib", 29 | "pyreadline3", 30 | ], 31 | classifiers=[ 32 | "Programming Language :: Python :: 3", 33 | "License :: OSI Approved :: MIT License", 34 | "Operating System :: OS Independent", 35 | ], 36 | python_requires=">=3.10", 37 | entry_points={ 38 | "console_scripts": [ 39 | "asteroid = asteroid:main", 40 | ], 41 | }, 42 | ) 43 | -------------------------------------------------------------------------------- /asteroid/modules/pick.ast: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------ 2 | -- pick.ast 3 | -- 4 | -- this module implements the Pick structure 5 | -- 6 | -- (c) Lutz Hamel, University of Rhode Island 7 | ------------------------------------------------------------------ 8 | 9 | load system random. 10 | 11 | ------------------------------------------------------------------ 12 | structure Pick 13 | ------------------------------------------------------------------ 14 | -- A pick object holds a list of items that 15 | -- we can randomly pick from with the 'pickitems' 16 | -- member function 17 | 18 | with 19 | 20 | data items. 21 | 22 | function pickitems 23 | with none do 24 | let ix = random @randint(0, this @items @length()-1). 25 | return this @items @ix. 26 | with n:%integer do 27 | let l = []. 28 | for i in 1 to n do 29 | let ix = random @randint(0, this @items @length()-1). 30 | l @append(this @items @ix). 31 | end 32 | return l. 33 | end 34 | 35 | --make sure that we only pass lists into the constructor 36 | function __init__ with l:%list do 37 | let this @items = l. 38 | end 39 | 40 | end -- Pick 41 | 42 | ------------------------------------------------------------------ 43 | function pick 44 | ------------------------------------------------------------------ 45 | with l:%list do 46 | return Pick(l). 47 | end 48 | 49 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test078.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | function transpose with m do 4 | -- figure out the dimensions 5 | let xdim = m @0 @length(). 6 | let ydim = m @length(). 7 | 8 | -- reserve space for the transpose 9 | -- first we do the ydim of new matrix 10 | let mt = range(xdim). 11 | for y in mt do 12 | let mt @y = range(ydim). 13 | end 14 | 15 | -- swap the elements 16 | for x in range(xdim) do 17 | for y in range(ydim) do 18 | let mt @x @y = m @y @x. 19 | end 20 | end 21 | 22 | return mt. 23 | end 24 | 25 | function print_matrix with m do 26 | io @println "". 27 | for r in m do 28 | for e in r do 29 | io @print (tostring e + " "). 30 | end 31 | io @println (""). 32 | end 33 | io @println "". 34 | end 35 | 36 | let m = [[1,2], 37 | [3,4]]. 38 | 39 | let mt = transpose(m). 40 | 41 | io @println ("The transpose of:"). 42 | print_matrix m. 43 | io @println ("is:"). 44 | print_matrix mt. 45 | io @println (""). 46 | 47 | let m = [[1,2], 48 | [3,4], 49 | [5,6]]. 50 | 51 | let mt = transpose(m). 52 | 53 | io @println ("The transpose of:"). 54 | print_matrix m. 55 | io @println ("is:"). 56 | print_matrix mt. 57 | io @println (""). 58 | 59 | assert(mt == [[1,3,5],[2,4,6]]). 60 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test045.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system math. 3 | load system util. 4 | 5 | -- define common math functions locally so the 6 | -- formulas are easy to read 7 | let cos = math @cos. 8 | let sin = math @sin. 9 | let sqrt = math @sqrt. 10 | let pow = math @pow. 11 | let atan = math @atan. 12 | let pi = math @pi. 13 | 14 | function polar_to_cartesian with (r,psi) do 15 | -- return a tuple: (x,y) 16 | return (r*cos(psi),r*sin(psi)). 17 | end 18 | 19 | function cartesian_to_polar with (x,y) do 20 | return (sqrt(pow(x,2)+pow(y,2)),cartesian_to_psi(x,y)). 21 | end 22 | 23 | function cartesian_to_psi 24 | with (x,y) if x > 0 do 25 | return atan(toreal y/toreal x). 26 | with (x,y) if x < 0 and y >= 0 do 27 | return atan(toreal y/toreal x)+pi. 28 | with (x,y) if x < 0 and y < 0 do 29 | return atan(toreal y/toreal x)-pi. 30 | with (x,y) if x == 0 and y > 0 do 31 | return pi/2. 32 | with (x,y) if x == 0 and y < 0 do 33 | return -pi/2. 34 | with (x,y) if x == 0 and y == 0 do 35 | return none. 36 | end 37 | 38 | let (r,psi) = cartesian_to_polar(-3,5). 39 | let (x,y) = polar_to_cartesian(r,psi). 40 | 41 | io @println (x,y). 42 | 43 | -- show that the recovered coordinates are the same 44 | -- we started with 45 | assert (math @isclose(-3,x,0.0001) and math @isclose(5,y,0.0001)). 46 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | We welcome suggestions, contributions, and new collaborators. If have suggestions or bugs to report please contact us via [email](mailto:lutzhamel@uri.edu) or 4 | you can create an issue in our [GitHub repository](https://github.com/lutzhamel/asteroid). You can also engage with the Asteroid community 5 | via our [Asteroid Forum](https://groups.google.com/g/asteroid-forum). If you would like to submit a 6 | pull request please read below. 7 | 8 | ## Pull Requests 9 | 10 | If you have a bugfix or feature implementation that you would like to submit via a pull request please do the following: 11 | * Send us an [email](mailto:lutzhamel@uri.edu) stating your intention and describing your bugfix or feature. 12 | * Add test cases to the testsuite that validate your bugfix or feature. 13 | * Make sure you run the **full** testsuite successfully, including the test cases for your bugfix or feature, before submitting your pull request. 14 | * Submit your pull request once you hear back from us. 15 | 16 | ## Becoming a Collaborator 17 | 18 | This is an open source project and we are always looking for and rely on motivated collaborators to take our project to the next level. In order to 19 | become a collaborator you have to submit three distinct and successful pull requests. As a collaborator you will have developer access to the 20 | repository and will become a member of our Slack channel where in-depth feature discussions take place and long-term strategies are being discussed. 21 | -------------------------------------------------------------------------------- /asteroid/test-suites/ug-programs/prgm-044.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | let pos_num = pattern %[x if isscalar(x) and x > 0]%. 4 | 5 | structure Asteroid with 6 | data size. 7 | function __init_ 8 | with v:*pos_num do 9 | let this@size = v. 10 | end 11 | end 12 | 13 | structure Spaceship with 14 | data size. 15 | function __init_ 16 | with v:*pos_num do 17 | let this@size = v. 18 | end 19 | end 20 | 21 | -- we use first-class pattern SpaceObject to 22 | -- express that both asteroids and space ships are space objects. 23 | let SpaceObject = pattern %[x if (x is %Asteroid) or (x is %Spaceship)]%. 24 | 25 | -- multi-dispatch function 26 | function collide_with 27 | with (a:%Asteroid, b:%Spaceship) do 28 | return "a/s". 29 | with (a:%Spaceship, b:%Asteroid) do 30 | return "s/a". 31 | with (a:%Spaceship, b:%Spaceship) do 32 | return "s/s". 33 | with (a:%Asteroid, b:%Asteroid) do 34 | return "a/a". 35 | end 36 | 37 | -- here we use the first-class pattern SpaceObject as a 38 | -- constraint on the function parameters. 39 | function collide with (x:*SpaceObject, y:*SpaceObject) do 40 | return "Big boom!" if (x@size > 100 and y@size > 100) else collide_with(x, y). 41 | end 42 | 43 | io @println (collide(Asteroid(101), Spaceship(300))). 44 | io @println (collide(Asteroid(10), Spaceship(10))). 45 | io @println (collide(Spaceship(101), Spaceship(10))). 46 | -------------------------------------------------------------------------------- /.github/workflows/generate_documentation.yml: -------------------------------------------------------------------------------- 1 | # this script throws an error when trying to commit the generated documentation 2 | # right now set to 'none' branch that will not match anything in this repo 3 | name: Documentation Generation 4 | on: 5 | pull_request: 6 | branches: 7 | #- 'dev-**' 8 | - 'none' 9 | 10 | push: 11 | branches: 12 | #- 'dev-**' 13 | - 'none' 14 | env: 15 | PYTHONPATH: ${{ github.workspace }} 16 | jobs: 17 | gen-docs: 18 | runs-on: ubuntu-latest 19 | steps: 20 | - name: Check out repository 21 | uses: actions/checkout@v2 22 | 23 | - uses: actions/checkout@v2 24 | with: 25 | persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal access token. 26 | fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository. 27 | 28 | - name: Regenerate documentation 29 | run: | 30 | pip install pcpp 31 | cd docs 32 | # This is to keep github actions happy 33 | python generate_docs.py 34 | cd .. 35 | 36 | - name: Commit files 37 | run: | 38 | git add . 39 | git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com" 40 | git config --local user.name "github-actions[bot]" 41 | (git commit -m "Updated documentation") || true 42 | 43 | - name: Push changes 44 | uses: ad-m/github-push-action@master 45 | with: 46 | github_token: ${{ secrets.GITHUB_TOKEN }} 47 | branch: ${{ github.ref }} 48 | 49 | 50 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test109.ast: -------------------------------------------------------------------------------- 1 | -- Testing list member functions 2 | 3 | let arr = []. 4 | 5 | -- length 6 | assert([1,2,3] @length() == 3). 7 | 8 | -- append 9 | arr @append(1). 10 | assert(arr == [1]). 11 | 12 | -- extend 13 | arr @extend([2,3,4]). 14 | assert(arr == [1,2,3,4]). 15 | 16 | -- insert 17 | arr @insert(0,0). 18 | assert(arr == [0,1,2,3,4]). 19 | 20 | -- remove 21 | arr @remove(4). 22 | assert(arr == [0,1,2,3]). 23 | 24 | -- pop 25 | arr @pop(). 26 | assert(arr == [0,1,2]). 27 | 28 | -- clear 29 | arr @clear(). 30 | assert(arr == []). 31 | 32 | -- index 33 | arr @extend([10,9,8,7,6]). 34 | assert(arr @index(6) == 4). 35 | 36 | -- count 37 | arr @extend([0,0,0,0,0]). 38 | assert(arr @count(0) == 5). 39 | 40 | -- sort 41 | arr @clear(). 42 | arr @extend([5,3,2,1,4,0]). 43 | assert(arr @sort() == [0,1,2,3,4,5]). 44 | 45 | -- reverse 46 | assert(arr @reverse() == [5,4,3,2,1,0]). 47 | 48 | -- copy 49 | assert(arr @copy() == arr). 50 | 51 | -- shuffle 52 | assert(arr @shuffle() @sort() == arr @sort()). 53 | 54 | -- map 55 | let twotimes = arr @map(lambda with x do return 2*x). 56 | assert(twotimes == [0,2,4,6,8,10]). 57 | 58 | -- reduce 59 | let sum = 0+1+2+3+4+5. 60 | let map_sum = arr @reduce(lambda with (x,y) do return x+y). 61 | assert(sum == map_sum). 62 | 63 | -- filter 64 | let arr = [0 to 10]. 65 | function gtfive 66 | with (x:%integer) if x > 5 do 67 | return true. 68 | 69 | with _ do 70 | return false. 71 | end 72 | 73 | assert(arr @filter(gtfive) == [6,7,8,9,10]). 74 | 75 | -- member 76 | assert(arr @member(10)). 77 | 78 | -- join 79 | let arr = ["a", "b", "c", "d", "efg"]. 80 | assert(arr @join("") == "abcdefg"). -------------------------------------------------------------------------------- /asteroid/state.py: -------------------------------------------------------------------------------- 1 | ########################################################################################### 2 | # Asteroid State Object 3 | # 4 | # (c) University of Rhode Island 5 | ########################################################################################### 6 | 7 | from asteroid.symtab import SymTab 8 | 9 | class State: 10 | def __init__(self): 11 | self.initialize() 12 | 13 | def initialize(self,module=""): 14 | self.mainmodule = None 15 | self.symbol_table = SymTab() 16 | self.AST = None 17 | self.eval_redundancy = True 18 | self.warning = True # switch for general warnings 19 | self.lineinfo = (module, 1) # tuple: module, lineno 20 | # stack of 3-tuples for stack trace of function 21 | # calls: (module,lineno,function name) 22 | self.trace_stack = [(module,1,"")] 23 | # if an exception occurs then error_trace will point to 24 | # it. an exception handler is responsible for clearing 25 | # this. 26 | self.error_trace = None 27 | self.debugger = None 28 | self.repl_use_autocompletion = True 29 | 30 | state = State() 31 | 32 | def warning(str): 33 | if state.warning: 34 | module, lineno = state.lineinfo 35 | print("Warning: {}: {}: {}".format(module, lineno, str)) 36 | 37 | def dump_trace(): 38 | if state.error_trace: 39 | _dump_trace(state.error_trace) 40 | return 41 | else: 42 | _dump_trace(state.trace_stack) 43 | return 44 | 45 | def _dump_trace(trace): 46 | print("traceback (most recent call last):") 47 | for i in range(0,len(trace)): 48 | (module,lineno,fname) = trace[i] 49 | print("{}: {}: calling {}".format(module,lineno,fname)) 50 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test068.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | load system random. 3 | 4 | random @seed(42). 5 | 6 | -- Asteroid allows the user to escape to the Python 7 | -- ecosystem. Here we use this to construct a 8 | -- plot of the moving average 9 | function plot with (dt,avg) do escape 10 | " 11 | import pandas as pd 12 | import matplotlib.pyplot as plt 13 | 14 | # import parameters from Asteroid into Python 15 | dt_val = state.symbol_table.lookup_sym('dt') 16 | avg_val = state.symbol_table.lookup_sym('avg') 17 | 18 | # convert from Asteroid lists to Python lists 19 | dt_plot = [] 20 | for (_,v) in dt_val[1]: 21 | dt_plot.append(v) 22 | 23 | avg_plot = [] 24 | for (_,v) in avg_val[1]: 25 | avg_plot.append(v) 26 | 27 | # construct a dataframe to plot 28 | df = pd.DataFrame(data={'Data':dt_plot,'Average':avg_plot}) 29 | 30 | # colors for the line plot 31 | colors = ['steelblue', 'red'] 32 | 33 | # line plot 34 | df.plot(color=colors, linewidth=3, figsize=(12,6)) 35 | 36 | # modify ticks size 37 | plt.xticks(fontsize=14) 38 | plt.yticks(fontsize=14) 39 | plt.legend(labels =['Data', 'Moving Average'], fontsize=14) 40 | 41 | # title and labels 42 | plt.title('Moving Average', fontsize=20) 43 | plt.xlabel('Instance', fontsize=16) 44 | plt.ylabel('Value', fontsize=16) 45 | #plt.show() 46 | plt.savefig('action-tests/action-test68.png') 47 | " 48 | end 49 | 50 | -- compute the window on lst 51 | function window with (lst:%list,i:%integer) if i >= 3 and i <= lst @length()-4 do 52 | return lst @[i-3,i-2,i-1,i,i+1,i+2,i+3]. 53 | end 54 | 55 | -- sum the values of lst 56 | function sum with lst:%list do 57 | return lst @reduce(lambda with (x,y) do return x+y). 58 | end 59 | 60 | let dt = [1 to 100] @map(lambda with _ do return random @random()). 61 | let mavg = [3 to 96] @map(lambda with i do return sum(window(dt,i))/toreal 7). 62 | 63 | plot(dt @[3 to 96],mavg). 64 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test116.ast: -------------------------------------------------------------------------------- 1 | -- Tests for file i/o 2 | 3 | load system io. 4 | 5 | let filename = escape 6 | " 7 | global __retval__ 8 | 9 | import tempfile 10 | 11 | new_file, filename = tempfile.mkstemp() 12 | 13 | __retval__ = ('string', filename) 14 | ". 15 | 16 | -- Open up the temporary file for appending 17 | let file = io @open(filename, "a"). 18 | 19 | -- Write some content and close the file 20 | io @writeln(file, "Asteroid!"). 21 | io @write(file, "Asteroid!!"). 22 | io @write(file, "Asteroid!!!"). 23 | io @close(file). 24 | 25 | ---------------------------------------------------- 26 | 27 | -- Open the file for reading 28 | let file = io @open(filename, "r"). 29 | 30 | -- Seek to the next line 31 | io @readln(file). 32 | 33 | -- Assert that the remaining content is the last two lines 34 | assert (io @read(file) == "Asteroid!!Asteroid!!!"). 35 | 36 | -- Close the file 37 | io @close(file). 38 | 39 | ---------------------------------------------------- 40 | 41 | -- Check that print(X) is write(__STDOUT__, X) 42 | io @print("Hello,"). 43 | io @write(io @__STDOUT__, " world!" + "\n"). 44 | 45 | -- Check that io @println(X) is writeln(__STDOUT__, X) 46 | io @println("First-class patterns"). 47 | io @writeln(io @__STDOUT__, "are pretty cool!"). 48 | 49 | -- Check that input() is the same as read(__STDIN__). 50 | io @println("Please enter your name: "). 51 | let name = io @readln(io @__STDIN__). 52 | io @println("Hello, " + name). 53 | 54 | -- Write to STDERR 55 | io @writeln(io @__STDERR__, "Oh no, you've encountered an error!"). 56 | io @writeln(io @__STDERR__, "^ This is just a test. You're doing fine"). 57 | 58 | 59 | ---------------------------------------------------- 60 | 61 | -- Test implicit write and read 62 | io @writeln("What's your favorite color?"). 63 | let x = io @readln(). 64 | io @write("Wow! I like "). 65 | io @writeln(x + " too!"). 66 | -------------------------------------------------------------------------------- /docs/Installing and Running.rst: -------------------------------------------------------------------------------- 1 | .. highlight:: none 2 | 3 | Installation 4 | ============ 5 | 6 | Asteroid is available from the PyPI project website 7 | `pypi.org/project/asteroid-lang `_ 8 | and is installed using:: 9 | 10 | $ pip install asteroid-lang 11 | 12 | This should work on Unix-like and Windows systems, 13 | though you may have to use `pip3` or some other variation. 14 | 15 | Don't forget to add the `bin` directory where `pip` installs programs 16 | to your `PATH` variable. 17 | 18 | In addition to installing Asteroid directly on your machine, 19 | there is also a cloud-based Linux virtual machine that is completely 20 | set up with an Asteroid environment and can be accessed at 21 | `Repl.it `_. 22 | 23 | Running the Asteroid Interpreter 24 | ================================ 25 | 26 | You can now run the interpreter from the command line by simply typing `asteroid`. 27 | This will work on both Windows and Unix-like systems as long as you followed the instructions above. 28 | To run asteroid on Unix-like systems and on our virtual machine, 29 | :: 30 | 31 | $ cat hello.ast 32 | -- the obligatory hello world program 33 | 34 | load system io. 35 | 36 | io @println "Hello, World!". 37 | 38 | $ asteroid hello.ast 39 | Hello, World! 40 | $ 41 | 42 | On Windows 10 the same thing looks like this, 43 | :: 44 | 45 | C:\> type hello.ast 46 | -- the obligatory hello world program 47 | 48 | load system io. 49 | 50 | io @println "Hello, World!". 51 | 52 | C:\> asteroid hello.ast 53 | Hello, World! 54 | C:\> 55 | 56 | 57 | As you can see, once you have Asteroid installed on your system you can execute an 58 | Asteroid program by typing:: 59 | 60 | asteroid [flags] 61 | 62 | at the command prompt. Asteroid also supports an interactive mode:: 63 | 64 | $ asteroid 65 | Asteroid Version x.y.z 66 | Type "asteroid -h" for help 67 | Press CTRL-D to exit 68 | ast> load system io. 69 | ast> io @println "Hello, World!". 70 | Hello, World! 71 | ast> 72 | -------------------------------------------------------------------------------- /asteroid/modules/random.ast: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------ 2 | -- random.ast 3 | -- 4 | -- this module implements the random numbers 5 | -- 6 | -- (c) University of Rhode Island 7 | ------------------------------------------------------------------ 8 | 9 | ------------------------------------------------------------------ 10 | function random 11 | ------------------------------------------------------------------ 12 | -- return a random floating point number in the range [0.0, 1.0). 13 | with none do return escape 14 | " 15 | import random 16 | global __retval__ 17 | 18 | __retval__ = ('real', random.random()) 19 | " 20 | end 21 | 22 | ------------------------------------------------------------------ 23 | function randint -- random interval 24 | ------------------------------------------------------------------ 25 | -- return a random value N in the interval lo <= N <= hi. 26 | -- The type of the random value depends on the types of the 27 | -- values specifying the interval. 28 | with (lo:%integer,hi:%integer) do return escape 29 | " 30 | import random 31 | from asteroid.support import assert_match 32 | global __retval__ 33 | 34 | lo_val = state.symbol_table.lookup_sym('lo') 35 | hi_val = state.symbol_table.lookup_sym('hi') 36 | 37 | __retval__ = ('integer', random.randint(lo_val[1], hi_val[1])) 38 | " 39 | with (lo:%real,hi:%real) do return escape 40 | " 41 | import random 42 | from asteroid.support import assert_match 43 | global __retval__ 44 | 45 | lo_val = state.symbol_table.lookup_sym('lo') 46 | hi_val = state.symbol_table.lookup_sym('hi') 47 | 48 | __retval__ = ('real', random.uniform(lo_val[1], hi_val[1])) 49 | " 50 | with (_,_) do 51 | throw Error("unsupported interval specification in randint"). 52 | end 53 | 54 | ------------------------------------------------------------------ 55 | function seed 56 | ------------------------------------------------------------------ 57 | -- provide a seed value for the random number generator 58 | with (sd:%integer) do escape 59 | " 60 | import random 61 | from asteroid.support import assert_match 62 | 63 | sd_val = state.symbol_table.lookup_sym('sd') 64 | random.seed(sd_val[1]) 65 | " 66 | end 67 | 68 | -------------------------------------------------------------------------------- /asteroid/modules/vector.ast: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------ 2 | -- vector.ast 3 | -- 4 | -- this module defines functions useful for vector arithmetic 5 | -- 6 | -- (c) University of Rhode Island 7 | ------------------------------------------------------------------ 8 | 9 | load system util. 10 | 11 | ------------------------------------------------------------------ 12 | function add 13 | ------------------------------------------------------------------ 14 | with (a,b) do 15 | return op(lambda with (x,y) do x+y,a,b). 16 | end 17 | 18 | ------------------------------------------------------------------ 19 | function dot 20 | -- computes the dot product of two lists 21 | ------------------------------------------------------------------ 22 | with (a:%list,b:%list) do 23 | return mult(a,b) @reduce(lambda with (x,y) do x+y). 24 | end 25 | 26 | ------------------------------------------------------------------ 27 | function mult 28 | ------------------------------------------------------------------ 29 | with (a,b) do 30 | return op(lambda with (x,y) do x*y,a,b). 31 | end 32 | 33 | ------------------------------------------------------------------ 34 | function op 35 | ------------------------------------------------------------------ 36 | -- worker function for actual vector arithmetic 37 | -- Note: it also implements vector/scalar arithmetic 38 | with (f:%function,a:%list,b:%list) do 39 | if a @length() =/= b @length() do 40 | throw Error("vector operations only defined on lists of the same length"). 41 | end 42 | let v = util @zip(a,b). 43 | return v @map(f). 44 | with (f:%function,a:%list,b if isscalar b) do 45 | -- promote b to a vector 46 | let bv = [1 to a @length()] @map(lambda with _ do b). 47 | let v = util @zip(a,bv). 48 | return v @map(f). 49 | with (f:%function,a if isscalar a,b:%list) do 50 | -- promote a to a vector 51 | let av = [1 to b @length()] @map(lambda with _ do a). 52 | let v = util @zip(av,b). 53 | return v @map(f). 54 | end 55 | 56 | ------------------------------------------------------------------ 57 | function sub 58 | ------------------------------------------------------------------ 59 | with (a,b) do 60 | return op(lambda with (x,y) do x-y,a,b). 61 | end 62 | 63 | 64 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test134.ast: -------------------------------------------------------------------------------- 1 | load system io. 2 | 3 | function foo 4 | with %pattern do 5 | io @println("is a pattern"). 6 | with _ do 7 | io @println("not a pattern"). 8 | end 9 | 10 | foo (pattern x). 11 | foo (lambda with x do return x). 12 | 13 | function foo 14 | with %pattern do 15 | io @println("is a pattern"). 16 | with %function do 17 | io @println("not a pattern"). 18 | end 19 | 20 | foo (pattern x). 21 | foo (lambda with x do return x). 22 | 23 | 24 | function foo 25 | with %function do 26 | io @println("not a pattern"). 27 | with %pattern do 28 | io @println("is a pattern"). 29 | end 30 | 31 | foo (pattern x). 32 | foo (lambda with x do return x). 33 | 34 | function foo 35 | with %pattern do 36 | io @println("not a pattern"). 37 | with %pattern do 38 | io @println("is a pattern"). 39 | end 40 | 41 | try 42 | foo (pattern x). 43 | throw Error("Redundanct pattern not detected!"). 44 | catch e do 45 | io @println("Success!"). 46 | end 47 | 48 | let x:%pattern = 1. 49 | 50 | 51 | function foo 52 | with %pattern do 53 | io @println("not a pattern"). 54 | with %integer do 55 | io @println("is a pattern"). 56 | end 57 | 58 | try 59 | foo (pattern x). 60 | foo (1). 61 | catch e do 62 | io @println("Success!"). 63 | end 64 | 65 | function foo 66 | with %pattern do 67 | io @println("not a pattern"). 68 | with %real do 69 | io @println("is a pattern"). 70 | end 71 | 72 | try 73 | foo (pattern x). 74 | foo (1.1). 75 | catch e do 76 | io @println("Success!"). 77 | end 78 | 79 | function foo 80 | with %integer do 81 | io @println("not a pattern"). 82 | with %pattern do 83 | io @println("is a pattern"). 84 | end 85 | 86 | 87 | foo (pattern x). 88 | foo (1). 89 | 90 | function foo 91 | with %pattern do 92 | io @println("not a pattern"). 93 | with %string do 94 | io @println("is a pattern"). 95 | end 96 | 97 | try 98 | foo (pattern x). 99 | foo ("hello"). 100 | catch e do 101 | io @println("Success!"). 102 | end -------------------------------------------------------------------------------- /asteroid/modules/set.ast: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------ 2 | -- set.ast 3 | -- 4 | -- this module implements Asteroid sets as lists. The big 5 | -- between lists and sets is that sets do not have repeated 6 | -- members. 7 | -- 8 | -- (c) Lutz Hamel, University of Rhode Island 9 | ------------------------------------------------------------------ 10 | 11 | ------------------------------------------------------------------ 12 | function diff 13 | ------------------------------------------------------------------ 14 | with (a:%list,b:%list) do return escape 15 | " 16 | global __retval__ 17 | a_val = state.symbol_table.lookup_sym('a') 18 | b_val = state.symbol_table.lookup_sym('b') 19 | __retval__ = ('list', list(set(a_val[1]) - set(b_val[1]))) 20 | " 21 | end 22 | 23 | ------------------------------------------------------------------ 24 | function intersection 25 | ------------------------------------------------------------------ 26 | with (a:%list,b:%list) do return escape 27 | " 28 | global __retval__ 29 | a_val = state.symbol_table.lookup_sym('a') 30 | b_val = state.symbol_table.lookup_sym('b') 31 | __retval__ = ('list', list(set(a_val[1]) & set(b_val[1]))) 32 | " 33 | end 34 | 35 | ------------------------------------------------------------------ 36 | function toset 37 | ------------------------------------------------------------------ 38 | with (lst:%list) do return escape 39 | " 40 | global __retval__ 41 | lst_val = state.symbol_table.lookup_sym('lst') 42 | __retval__ = ('list', list(set(lst_val[1]))) 43 | " 44 | end 45 | 46 | ------------------------------------------------------------------ 47 | function union 48 | ------------------------------------------------------------------ 49 | with (a:%list,b:%list) do return escape 50 | " 51 | global __retval__ 52 | a_val = state.symbol_table.lookup_sym('a') 53 | b_val = state.symbol_table.lookup_sym('b') 54 | __retval__ = ('list', list(set(a_val[1]) | set(b_val[1]))) 55 | " 56 | end 57 | 58 | ------------------------------------------------------------------ 59 | function xunion 60 | -- elements in a or b but not both 61 | ------------------------------------------------------------------ 62 | with (a:%list,b:%list) do return escape 63 | " 64 | global __retval__ 65 | a_val = state.symbol_table.lookup_sym('a') 66 | b_val = state.symbol_table.lookup_sym('b') 67 | __retval__ = ('list', list(set(a_val[1]) ^ set(b_val[1]))) 68 | " 69 | end 70 | 71 | -------------------------------------------------------------------------------- /asteroid/test-suites/programs/tokenizer.ast: -------------------------------------------------------------------------------- 1 | -- a simple tokenizer for our calc language 2 | -- it take a string as input and returns a stream of tokens as output. 3 | -- it uses the Stream structure available from util and demonstrates 4 | -- how regular expression can be used during pattern matching. 5 | -- Lutz Hamel, (c) University of Rhode Island 6 | 7 | load system stream. 8 | 9 | structure Token with 10 | data type. 11 | data value. 12 | end 13 | 14 | function get_number 15 | with input_stream do 16 | let number = "". 17 | while input_stream @peek() is "[0-9]" do 18 | let number = number + input_stream @get(). 19 | end 20 | return tointeger(number). 21 | end 22 | 23 | function tokenize 24 | with input:%string do 25 | let input_stream = stream @stream(input @explode()). 26 | let output_stream = stream @stream([]). 27 | while not input_stream @eof() do 28 | let c = input_stream @peek(). 29 | if c is "[0-9]" do 30 | let n = get_number(input_stream). 31 | let token = Token("number",n). 32 | output_stream @append(token). 33 | elif c is "\+" do 34 | input_stream @get(). 35 | let token = Token("add","+"). 36 | output_stream @append(token). 37 | elif c is "-" do 38 | input_stream @get(). 39 | let token = Token("sub","-"). 40 | output_stream @append(token). 41 | elif c is "\*" do 42 | input_stream @get(). 43 | let token = Token("mul","*"). 44 | output_stream @append(token). 45 | elif c is "/" do 46 | input_stream @get(). 47 | let token = Token("div","/"). 48 | output_stream @append(token). 49 | elif c is "\(" do 50 | input_stream @get(). 51 | let token = Token("lparen","("). 52 | output_stream @append(token). 53 | elif c is "\)" do 54 | input_stream @get(). 55 | let token = Token("rparen",")"). 56 | output_stream @append(token). 57 | elif c is "[\n\t ]" do 58 | input_stream @get(). -- get and ignore 59 | else do 60 | throw Error("unknown symbol: " + c). 61 | end 62 | end 63 | return output_stream. 64 | end 65 | 66 | if toplevel() do 67 | -- test harness for tokenizer 68 | load system io. 69 | let output_stream = tokenize("(101+1)*2"). 70 | let output_stream = tokenize("101"). 71 | io @ println (tostring output_stream). 72 | end 73 | -------------------------------------------------------------------------------- /asteroid/test-suites/python-tests/repl_test.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | 4 | #Code from run-tests.py 5 | file_path = os.path.dirname(os.path.abspath( __file__ )) 6 | os.chdir(file_path) 7 | (parent_dir,_) = os.path.split(file_path) 8 | #We need the granparent dir, since we're 2 layers down from asteroid 9 | (grandparent_dir,_) = os.path.split(parent_dir) 10 | sys.path.append(grandparent_dir) 11 | 12 | from asteroid.interp import interp 13 | import asteroid.lex as lex 14 | from asteroid.state import state 15 | 16 | def test_get_identifiers(): 17 | print(lex.get_indentifiers()) 18 | interp("load io.", "test") 19 | print(lex.get_indentifiers()) 20 | interp("let aaa = 1.", "test") 21 | interp("let aab = 2.", "test", initialize_state=False, prologue=False) 22 | interp("let abc = 3.", "test", initialize_state=False, prologue=False) 23 | interp("let bc = 4.", "test", initialize_state=False, prologue=False) 24 | interp("let baa = 5.", "test", initialize_state=False, prologue=False) 25 | print(lex.get_indentifiers()) 26 | 27 | def test_get_identifiers_by_prefix(): 28 | print(lex.get_indentifiers()) 29 | interp("load io.", "test") 30 | print(lex.get_indentifiers()) 31 | interp("let aaa = 1.", "test") 32 | interp("let aab = 2.", "test", initialize_state=False, prologue=False) 33 | interp("let abc = 3.", "test", initialize_state=False, prologue=False) 34 | interp("let bc = 4.", "test", initialize_state=False, prologue=False) 35 | interp("let baa = 5.", "test", initialize_state=False, prologue=False) 36 | print(lex.get_indentifiers()) 37 | print(lex.get_indentifiers_by_prefix("a")) 38 | print(lex.get_indentifiers_by_prefix("aa")) 39 | print(lex.get_indentifiers_by_prefix("__")) 40 | 41 | def test_get_member_identifiers(): 42 | interp("load io.") 43 | interp('structure A with\n\tdata a.\n\tdata b.\nend.', initialize_state=False, prologue=False) 44 | interp('let foo = A(1,2).', initialize_state=False, prologue=False) 45 | print(lex.get_member_identifiers("io")) 46 | print(lex.get_member_identifiers("A")) 47 | print(lex.get_member_identifiers("foo")) 48 | return None 49 | 50 | if __name__ == "__main__": 51 | test_get_member_identifiers() 52 | print("Testing get_identifiers:\n") 53 | test_get_identifiers() 54 | print("\n\n\n") 55 | print("Testing get_identifiers_by_prefix:\n") 56 | test_get_identifiers_by_prefix() 57 | print("\n\n\n") 58 | print("Testing get_member_identifiers:\n") 59 | test_get_member_identifiers() 60 | print("\n\nTesting complete.") 61 | 62 | -------------------------------------------------------------------------------- /asteroid/modules/stream.ast: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------ 2 | -- stream.ast 3 | -- 4 | -- this module implements a Stream structure that allows the 5 | -- user to turn any list into a stream with supporting operations 6 | -- like 'peek', 'get', and 'eof'. 7 | -- 8 | -- (c) University of Rhode Island 9 | ------------------------------------------------------------------ 10 | 11 | load system util. 12 | 13 | ------------------------------------------------------------------ 14 | structure Stream 15 | ------------------------------------------------------------------ 16 | -- Asteroid stream implementation based on lists 17 | with 18 | 19 | data stream. 20 | data curr_ix. 21 | 22 | function __init__ 23 | with stream:%list do 24 | let this @stream = stream @copy(). -- shallow copy of input list 25 | let this @curr_ix = 0. 26 | end 27 | 28 | function append 29 | with item do 30 | this @stream @append item. 31 | end 32 | 33 | function eof 34 | with none do 35 | if this @curr_ix == this @stream @length() do 36 | return true. 37 | else do 38 | return false. 39 | end 40 | end 41 | 42 | function get 43 | with none do 44 | if this @eof() do 45 | return none. 46 | else do 47 | let v = this @stream @(this @curr_ix). 48 | let this @curr_ix = this @curr_ix + 1. 49 | return v. 50 | end 51 | end 52 | 53 | function map 54 | with f:%function do 55 | for i in range(this @stream @length()) do 56 | let this @stream @i = f(this @stream @i). 57 | end 58 | end 59 | 60 | function peek 61 | with none do 62 | if this @eof() do 63 | return none. 64 | else do 65 | return this @stream @(this @curr_ix). 66 | end 67 | end 68 | 69 | function rewind 70 | with none do 71 | let this @curr_ix = 0. 72 | end 73 | 74 | function __str__ 75 | with none do return 76 | escape 77 | " 78 | from asteroid.state import state 79 | global __retval__ 80 | 81 | this_val = state.symbol_table.lookup_sym('this') 82 | 83 | # destructure object 84 | (OBJECT, 85 | (STRUCT_ID, (ID, struct_id)), 86 | (MEMBER_NAMES, (LIST, member_names)), 87 | (OBJECT_MEMORY, (LIST, memory))) = this_val 88 | 89 | __retval__ = ('string', term2string(memory[0])) 90 | " 91 | end 92 | 93 | end -- Stream structure 94 | 95 | 96 | ------------------------------------------------------------------ 97 | function stream 98 | ------------------------------------------------------------------ 99 | with l:%list do 100 | return Stream(l). 101 | end 102 | 103 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | # Configuration file for the Sphinx documentation builder. 2 | # 3 | # This file only contains a selection of the most common options. For a full 4 | # list see the documentation: 5 | # https://www.sphinx-doc.org/en/master/usage/configuration.html 6 | 7 | # -- Path setup -------------------------------------------------------------- 8 | 9 | # If extensions (or modules to document with autodoc) are in another directory, 10 | # add these directories to sys.path here. If the directory is relative to the 11 | # documentation root, use os.path.abspath to make it absolute, like shown here. 12 | # 13 | # import os 14 | # import sys 15 | # sys.path.insert(0, os.path.abspath('.')) 16 | 17 | 18 | # -- Project information ----------------------------------------------------- 19 | 20 | project = 'Asteroid' 21 | copyright = '2021- University of Rhode Island' 22 | author = 'Lutz Hamel, Tim Colaneri, Oliver McLaughlin, Ariel Finkel, Theodore Henson, Calvin Higgins, Christian Tropeano' 23 | 24 | # The full version, including alpha/beta/rc tags 25 | # when generating docs make sure that PYTHONPATH points 26 | # to the root directory of the github repo 27 | # readthedocs does not handle the full repo so we can't do this 28 | # from asteroid.version import VERSION 29 | # release = VERSION 30 | release = "2.0.2" 31 | 32 | 33 | # -- General configuration --------------------------------------------------- 34 | 35 | # Add any Sphinx extension module names here, as strings. They can be 36 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 37 | # ones. 38 | extensions = ['sphinx_rtd_theme',] 39 | #extensions = [] 40 | 41 | # Add any paths that contain templates here, relative to this directory. 42 | templates_path = ['_templates'] 43 | 44 | # The language for content autogenerated by Sphinx. Refer to documentation 45 | # for a list of supported languages. 46 | # 47 | # This is also used if you do content translation via gettext catalogs. 48 | # Usually you set "language" from the command line for these cases. 49 | language = 'english' 50 | 51 | # List of patterns, relative to source directory, that match files and 52 | # directories to ignore when looking for source files. 53 | # This pattern also affects html_static_path and html_extra_path. 54 | exclude_patterns = [] 55 | 56 | 57 | # -- Options for HTML output ------------------------------------------------- 58 | 59 | # The theme to use for HTML and HTML Help pages. See the documentation for 60 | # a list of builtin themes. 61 | # 62 | html_theme = 'sphinx_rtd_theme' 63 | 64 | # Add any paths that contain custom static files (such as style sheets) here, 65 | # relative to this directory. They are copied after the builtin static files, 66 | # so a file named "default.css" will overwrite the builtin "default.css". 67 | html_static_path = ['_static'] 68 | -------------------------------------------------------------------------------- /asteroid/test-suites/action-tests/test017.ast: -------------------------------------------------------------------------------- 1 | 2 | load system io. 3 | load system util. 4 | load system hash. 5 | 6 | -- text generated at 'https://www.lipsum.com/' 7 | let text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed 8 | accumsan magna quis risus commodo, et pellentesque dui cursus. Sed quis risus 9 | libero. Cras et mattis libero, eget varius nisi. Phasellus ultrices, augue non 10 | dictum eleifend, nunc elit blandit velit, a viverra risus enim in tellus. 11 | Maecenas quis ante eget turpis rhoncus rhoncus eget ut mauris. Suspendisse nec 12 | erat sed nunc tempus hendrerit. Nunc dictum nunc molestie eleifend tempus. 13 | Praesent cursus lorem diam, sed mattis velit vehicula scelerisque. Nunc iaculis 14 | rhoncus ante. Etiam quam nisi, fermentum et euismod a, vulputate eu elit. 15 | Suspendisse tincidunt ligula quis interdum blandit. Quisque sed aliquam tellus. 16 | Pellentesque ac lacus pulvinar, ornare purus ac, viverra ex. Donec quis pharetra 17 | dolor. 18 | 19 | In ac massa tortor. Cras sagittis luctus scelerisque. Morbi a neque sed tortor 20 | ultrices dapibus. Mauris pretium vitae massa non auctor. Cras egestas ex ante, 21 | ac ullamcorper ante dignissim eget. Fusce bibendum justo ut enim luctus, id 22 | volutpat diam lacinia. Mauris sit amet ante risus. 23 | 24 | Nullam rhoncus ultricies dui. Etiam vel metus vehicula, pellentesque felis ut, 25 | suscipit nunc. Sed nec interdum lorem. Maecenas odio erat, vestibulum nec 26 | dapibus id, commodo vitae libero. Nulla sed urna sit amet nunc commodo finibus 27 | sed vel elit. Aliquam euismod feugiat nisi quis placerat. Aliquam libero nisl, 28 | ultrices non est at, sagittis hendrerit dui. Quisque id sem lorem. Nam ultricies 29 | metus id ultrices molestie. Pellentesque elementum consequat nibh, nec convallis 30 | lorem ullamcorper in. Etiam vitae mi tellus. Etiam accumsan massa sit amet dolor 31 | tincidunt iaculis. Nam ullamcorper blandit sem id bibendum. Quisque elementum 32 | ipsum ac sapien blandit vehicula." 33 | 34 | -- get rid of punctuation, turn to lower case, and split into words. 35 | -- Note: we could have employed richer regular expressions to clean up the text here 36 | let wl = text @replace("\.","") 37 | @replace(",","") 38 | @tolower() 39 | @split(). 40 | 41 | -- put the words into a hash table, the value is the count of the words 42 | let ht = hash @hash(). 43 | for w in wl do 44 | if ht @get(w) is none do 45 | ht @insert(w,1). 46 | else do 47 | ht @insert(w,ht @get(w)+1). 48 | end 49 | end 50 | 51 | -- get the contents of hash table and find the most frequent word 52 | let (keys,values) = util @unzip(ht@aslist()). 53 | let values_sorted = values @copy() 54 | @sort(true). 55 | let most_frequent_word = keys @(values @index(values_sorted @0)). 56 | io @println most_frequent_word. 57 | 58 | assert (most_frequent_word == "sed"). 59 | -------------------------------------------------------------------------------- /asteroid/test-suites/regression-tests/test127.ast: -------------------------------------------------------------------------------- 1 | 2 | -- This tests is for ELIF line number reporting 3 | 4 | 5 | escape 6 | " 7 | from asteroid.interp import interp 8 | from asteroid.state import state 9 | 10 | print(1) 11 | 12 | program = '' 13 | program += 'let x = 1.\n' 14 | program += 'let y = 0.\n' 15 | program += '\n' 16 | program += 'if ( x < 0) do\n' 17 | program += ' ...\n' 18 | program += 'elif ( x / y < 1 ) do-- division by zero \n' 19 | program += ' ...\n' 20 | program += 'end\n' 21 | 22 | try: 23 | interp(program,exceptions=True) 24 | except: 25 | pass 26 | 27 | assert( state.lineinfo[1] == 6) 28 | 29 | print(2) 30 | 31 | program = '' 32 | program += 'let x = 1.\n' 33 | program += 'let y = 0.\n' 34 | program += '\n' 35 | program += 'function foo\n' 36 | program += ' with none do\n' 37 | program += ' if ( x < 0) do\n' 38 | program += ' ...\n' 39 | program += ' elif ( x / y < 1 ) do-- division by zero \n' 40 | program += ' ...\n' 41 | program += ' end\n' 42 | program += ' end\n' 43 | program += '\n' 44 | program += 'foo().\n' 45 | 46 | try: 47 | interp(program,exceptions=True) 48 | except: 49 | pass 50 | 51 | assert( state.lineinfo[1] == 8) 52 | 53 | print(3) 54 | 55 | program = '' 56 | program += 'let x = 1.\n' 57 | program += 'let y = 0.\n' 58 | program += '\n' 59 | program += 'function foo\n' 60 | program += ' with none do\n' 61 | program += ' if ( x < 0) do\n' 62 | program += ' ...\n' 63 | program += ' elif ( x < 0 ) do\n' 64 | program += ' ...\n' 65 | program += ' elif ( x < -1 ) do\n' 66 | program += ' ...\n' 67 | program += ' elif ( x / y < 1 ) do-- division by zero \n' 68 | program += ' ...\n' 69 | program += ' elif ( x < 10 ) do\n' 70 | program += ' ...\n' 71 | program += ' end\n' 72 | program += ' end\n' 73 | program += '\n' 74 | program += 'foo().\n' 75 | 76 | try: 77 | interp(program,exceptions=True) 78 | except: 79 | pass 80 | 81 | assert( state.lineinfo[1] == 12) 82 | 83 | print(4) 84 | 85 | program = '' 86 | program += 'let x = 1.\n' 87 | program += 'let y = 0.\n' 88 | program += '\n' 89 | program += 'while (true) do\n' 90 | program += ' if ( x < 0) do\n' 91 | program += ' ...\n' 92 | program += ' elif ( x < 1 ) do\n' 93 | program += ' ...\n' 94 | program += ' elif ( x < -1 ) do\n' 95 | program += ' ...\n' 96 | program += ' elif ( x < -2 ) do\n' 97 | program += ' ...\n' 98 | program += ' elif ( x / y < 1 ) do -- error\n' 99 | program += ' ...\n' 100 | program += ' elif ( x < -11 ) do\n' 101 | program += ' ...\n' 102 | program += ' elif ( x < 11 ) do\n' 103 | program += ' ...\n' 104 | program += ' end\n' 105 | program += 'end\n' 106 | 107 | try: 108 | interp(program,exceptions=True) 109 | except: 110 | pass 111 | 112 | assert( state.lineinfo[1] == 13 ) 113 | " 114 | -------------------------------------------------------------------------------- /asteroid/test-suites/run-tests.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # coding: utf-8 3 | ################################ 4 | # general testsuite driver 5 | ################################ 6 | 7 | # the following is a list of directories of test cases 8 | 9 | dirs = [ 10 | 'action-tests', 11 | 'programs', 12 | 'redundant-pattern-test', 13 | 'ref-programs', 14 | 'regression-tests', 15 | 'ug-programs', 16 | ] 17 | 18 | # the exclusion list allows you to eliminate certain tests from the 19 | # current run 20 | # TODO: make names test suite sensitive 21 | #exclusion_list = ['test015.ast'] 22 | exclusion_list = [] 23 | if exclusion_list: 24 | print("Exclusion list: {}".format(exclusion_list)) 25 | 26 | # set the following to True if you encounter failed test cases. It will 27 | # give you details and a stack dump. 28 | verbose_failure = False 29 | 30 | # control whether to do redundancy checks 31 | redundancy = True 32 | 33 | # if your test case needs input from stdin please provide 34 | # a file named, 35 | # 36 | # -io.txt 37 | # 38 | # For example, if your test case file is test-024.ast and it 39 | # requires input on stdin then provide a file test-024-io.txt 40 | # with one line of input for each require input. 41 | 42 | # TODO: capture stdout and compare it to a given 43 | # output file. 44 | 45 | import sys 46 | import os 47 | 48 | # Get the path of this file and temporarily change 49 | # the working directory to that path 50 | file_path = os.path.dirname(os.path.abspath( __file__ )) 51 | os.chdir(file_path) 52 | # append the parent dir of our current dir to the PYTHONPATH 53 | # so that we can easily find our Asteroid modules 54 | (parent_dir,_) = os.path.split(file_path) 55 | sys.path.append(parent_dir) 56 | from interp import interp 57 | 58 | n_tests = 0 59 | for d in dirs: 60 | # assemble the test list 61 | test_list = os.listdir(d) 62 | test_list = list(set(test_list) - set(exclusion_list)) 63 | test_list.sort() 64 | n_tests += len(test_list) 65 | # set the ASTEROIDPATH environment variable 66 | old_val = os.getenv('ASTEROIDPATH') 67 | os.environ['ASTEROIDPATH'] = d 68 | 69 | for testname in test_list: 70 | # check that we are actually looking at test case 71 | if testname[-3:] == "ast": 72 | # if a -io.txt file exists map stdin to it 73 | stdin_file = file_path+'/'+d+'/'+testname[0:-4]+"-io.txt" 74 | if os.path.exists(stdin_file): 75 | sys.stdin = open(stdin_file, "r") 76 | f = open(d+"/"+testname,"r") 77 | p = f.read() 78 | print("**********"+d+"/"+testname+"************") 79 | print(p) 80 | print("**********output***********") 81 | interp(p, 82 | exceptions=verbose_failure, 83 | redundancy=redundancy) 84 | f.close() 85 | if old_val: 86 | os.environ['ASTEROIDPATH'] = old_val 87 | else: 88 | del os.environ['ASTEROIDPATH'] 89 | 90 | print("Number of test cases:",n_tests) 91 | --------------------------------------------------------------------------------