Skip to content

Commit

Permalink
syntax change!
Browse files Browse the repository at this point in the history
  • Loading branch information
cassowarii committed Jun 8, 2018
1 parent 194daf3 commit 1577c56
Show file tree
Hide file tree
Showing 40 changed files with 192 additions and 144 deletions.
4 changes: 2 additions & 2 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ ACompileResult compile_wordseq(AScope *scope, AFuncRegistry *reg, AWordSeqNode *

ACompileResult r = compile_wordseq(scope_with_vars, reg, newbind->words, bindinfo_with_vars);

free_nameseq_node(current->data.bind->names);

if (r.status == compile_fail) {
free(newbind);
errors ++;
} else if (r.status == compile_success) {
/* Successfully compiled the inner scope, so alter the node. */
free_nameseq_node(current->data.bind->names);
free(current->data.bind);
current->type = var_bind;
current->data.vbind = newbind;
Expand Down
10 changes: 6 additions & 4 deletions examples/beer.alma
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# 99 Bottles of Beer

# prints correct pluralization of bottles
func bottle/s: if* [= 1] [print " bottle "] [print " bottles "] ;
fn bottle/s [ if* [= 1] [print " bottle "] [print " bottles "] ]

# string int → int, prints "X bottle(s) <msg>"
func quantified-bottles msg:
fn quantified-bottles msg [
if* [> 0] [print dup] [print "No more"]
bottle/s
println msg
]

# int → , sings the song for how-many verses
func bottles-of-beer how-many:
fn bottles-of-beer how-many [
how-many | while* [≥ 0] [
quantified-bottles "of beer on the wall"
quantified-bottles "of beer"
Expand All @@ -27,5 +28,6 @@ func bottles-of-beer how-many:
]
]
drop
]

func main: 99 | bottles-of-beer
fn main [ 99 | bottles-of-beer ]
9 changes: 5 additions & 4 deletions examples/closures.alma
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
func compose f1 f2: [ apply f1 apply f2 ]
fn compose f1 f2 [ [ apply f1 apply f2 ] ]

func make-sum-printer: compose [println] [+]
fn make-sum-printer [ compose [println] [+] ]

func make-adder x: [→ y: + x y]
fn make-adder x [ [→ y: + x y] ]

func main:
fn main [
apply (make-sum-printer 6 5) # Should print 11
make-adder 5 → 5adder (
println apply 5adder 3 # Should print 8
println apply 5adder 11 # Should print 16
println apply 5adder 76 # Should print 81
)
]
5 changes: 3 additions & 2 deletions examples/defining.alma
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
func main:
fn main [
print "The sum of 3, 4, and 5 is "
println sumof3 3 4 5
print "The sum of 32, 54, and 43 is "
println sumof3 32 54 43
]

func sumof3: + +
fn sumof3 [ + + ]
2 changes: 1 addition & 1 deletion examples/hello.alma
Original file line number Diff line number Diff line change
@@ -1 +1 @@
func main: println "Hello world!"
fn main [ say "Hello world!" ]
24 changes: 13 additions & 11 deletions examples/quicksort.alma
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
func empty: len | = 0
func small: len | ≤ 1
func concat: if* [= 0 len] [drop] [cons dip [concat] uncons]
func 2dip f: dip [dip f]
func when*: if* 2dip [ [] ]
fn empty [ len | = 0 ]
fn small [ len | ≤ 1 ]
fn concat [ if* [= 0 len] [drop] [cons dip [concat] uncons] ]
fn 2dip f [ dip [dip f] ]
fn when* [ if* 2dip [ [] ] ]

# Given an element and two lists, cons the element onto the second one.
func cons#2: swap | dip [cons]
fn cons#2 [ swap | dip [cons] ]

# Given an element and three lists, cons the element onto the third one.
func cons#3: swap | dip [cons#2]
fn cons#3 [ swap | dip [cons#2] ]

# Sort one element into the partitioned lists.
func sort-one-by comp: if* [apply comp] [cons#2] [cons#3] uncons
fn sort-one-by comp [ if* [apply comp] [cons#2] [cons#3] uncons ]

# Partition the list by repeatedly calling sort-one-by starting with two empty result lists.
func partition-by comp list: while* [not empty] [sort-one-by comp] list {} {} | drop
fn partition-by comp list [ while* [not empty] [sort-one-by comp] list {} {} | drop ]

# Quicksort: when list length > 1, partition, sort halves, and concatenate together.
func quicksort:
fn quicksort [
when* [not small] [ # only when list size > 1
uncons -> pivot (
partition-by [< pivot] # Partition list
Expand All @@ -27,8 +27,9 @@ func quicksort:
concat # concatenate halves together
)
]
]

func main:
fn main [
# Sort random numbers from 1 to 10
println quicksort { 3, 1, 6, 4, 10, 2, 5, 9, 8, 7 }

Expand All @@ -43,3 +44,4 @@ func main:
1, 7, 30, 71, 97, 38, 78, 11, 91, 50,
82, 93, 36, 68, 14, 8, 37, 42, 39, 9,
35, 12, 67, 13, 52, 89, 83, 2, 99, 16 }
]
10 changes: 6 additions & 4 deletions examples/square.alma
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
func main:
fn main [
let
func say-message :
fn say-message [
print "The "
print # prints message
print " of "
print dup
print " is "
]
in (
(say-message "double" | println double) 4
(say-message "square" | println square) 4
)
]

func double: * 2
func square: * dup
fn double [ * 2 ]
fn square [ * dup ]

6 changes: 4 additions & 2 deletions examples/triangle-numbers.alma
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Print the triangular numbers from 1 to 5000

func trianglenum:
fn trianglenum [
dip [0]
while* [> 0] [
dup | dip [+]
- 1
]
drop
]

func main:
fn main [
1
while* [≤ 5000] [
print "triangular number #"
Expand All @@ -17,3 +18,4 @@ func main:
println trianglenum dup
+ 1
]
]
2 changes: 1 addition & 1 deletion lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ IDONECH [^{}[\]()\"\' \t\n,;]|{UONLY}
"import" BEGIN(IMPORT); return T_IMPORT;
"let" return T_LET;
->|return T_BIND;
"func" return T_FUNC;
"fn" return T_FUNC;
"in" return T_IN;
\\[\r\n] ; // allow escaping newlines
<IMPORT>"as" { BEGIN(INITIAL); return T_AS; }
Expand Down
2 changes: 1 addition & 1 deletion lib_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void lib_quit(AStack* stack, AVarBuffer *buffer) {
void funclib_init(ASymbolTable *st, AScope *sc) {
addlibfunc(sc, st, "print", &lib_print);
addlibfunc(sc, st, "println", &lib_println);
addlibfunc(sc, st, ".", &lib_println); /* Alias */
addlibfunc(sc, st, "say", &lib_println); /* Alias */
addlibfunc(sc, st, "quit", &lib_quit);
addlibfunc(sc, st, "exit", &lib_quit);
}
Loading

0 comments on commit 1577c56

Please sign in to comment.