Skip to content

Instantly share code, notes, and snippets.

@mdg
Last active December 14, 2015 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdg/5095617 to your computer and use it in GitHub Desktop.
Save mdg/5095617 to your computer and use it in GitHub Desktop.
Pattern matching factorial function written in my very own programming language.
factorial :=
?(1) => 1
?(0) => 0
?(n) => n * factorial(n-1)
main() => write(@stdout, msg) where
result <- factorial(5)
msg <- "factorial(5) is $result\n"
[mdg]$ ./a.out T/patterns
factorial(5) is 120
[mdg]$
dfunc "factorial" 1
const $0 1
brne $0 %0 @case1
const result 1
return
@case1
const $1 0
brne $1 %0 @case2
const result 0
return
@case2
ref $5 %0
lfunc $2 "" "factorial"
ref $3 %0
const $4 1
isub $2.0 $3 $4
call $6 $2
imult result $5 $6
return
.func
dfunc "__main" 0
lfunc $1 "" "factorial"
const $1.0 5
call $0 $1
const $2 "factorial(5) is "
ref $3 $0
stracc $2 $3
const $3 "\n"
stracc $2 $3
lfunc $4 "core" "write"
lstackval $4.0 #stdout
ref $4.1 $2
call result $4
return
.func
@clutchski
Copy link

cool man. this is rad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment