Skip to content

TESTMECS/monkey-odin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

monkey

demo.monkey

#!/usr/bin/env monkey -- 1,2,3 T 24

# io
let arr = args(); # =>>["1", "2", "3", "T", "24"]
printf("args[1]='%d'", int->args()[2]); # ARROW Functions take 1 argument
let str_of_file = readf->"tests.txt";
let res = writef("tests.txt", "hello BANANAS")
# fn
let fibonacci = fn(x) {
  if (x == 0) {
    0
  } else {
    if (x == 1) {
      return 1;
    } else {
      fibonacci(x - 1) + fibonacci(x - 2);
    }
  }
};
fibonacci(5);

# for loops
let i = 0;
let a = [1, 2, 3, 4, 5];
for( i < len(a) ) {
	 a[i] = a[i] * 2;
   i = i + 1
}
a; #=>>[2, 4, 6, 8, 10]
# foreach loops
foreach i in [1,2,3,4] {
	puts(i);
}
let d = {"a": 1, "b": 2, "c": 3};
foreach k in reverse(keys(d)) {
	puts(k);
}
# macros
let check_positive = macro(x) {
	quote(if (unquote(x) > 0) { puts->"positive" } else { puts->"zero or negative" })
}
check_positive(5);                   # prints: positive  
check_positive(0);                   # prints: zero or negative

# types
let my_num = 12;
let my_float = 12.0;
let bool = bool(args()[1]);

# str
let my_str = "monkey BANANAS";
let my_str2 = upper(my_str);
let my_str3 = lower(my_str2);
puts(split(my_str3, " ")); # =>>["monkey", "BANANAS"]
# regex
let cap = match("hello monkey", "hello (.*)")
puts(cap); # =>> "monkey"

let new_str = replace("hello monkey", "hello (.*)", "BANANAS")
puts(new_str);

let test = contains("hello monkey", "monkey")
puts(test);

# arr
let my_arr = args(); # =>>["1", "2", "3"]
let arr2 = ["a", "b", "c"];
let my_num_arr = [1, 2, 3];
let v = slice(my_arr, 0, 2); # =>>["1", "2"]
indexOf(my_arr, "2"); # =>>1
sum->my_num_arr; # =>>6
min->my_num_arr; # =>>1
max->my_num_arr; # =>>3
puts->len(my_arr); # =>>3
#
# map
let map = map(arr2, my_arr); # {"a": 1, "b": 2, "c": 3}
let my_keys = keys->map; # =>["a", "b", "c"]
let my_values = values(map); # =>[1, 2, 3]
let my_has = has(map, "a"); # =>true
#
# math
let my_rand = rand();
let my_hash = hash->my_str;
let my_sin = sin->float(0);
let my_cos = cos->float(0);
let my_tan = tan->float(0);

# Bitwise ops
let bor = 1 | 2; puts->bor;
let bxor = 1 ^ 2; puts->bxor;
let band = 1 & 2; puts->band;
let bnot = ~1; puts->bnot;

let mod = 1 % 2; puts->mod;
let brshift = 4 >> 2; puts->brshift;
let blshift = 1 << 2; puts->blshift;

let lor = true || false; puts->lor;
let land = true && false; puts->land;
test_dir := "./examples/"
exe := "./monkey-odin.out"
test_file := "arrow_call.monkey"
tree_sitter_parse_test_file := "./examples/demo.monkey"

default:
	@just --list
alias t := test
test:
	echo "Running test {{test_file}}"
	time {{exe}} file {{test_dir}}{{test_file}} 
alias b := build
build:
	time odin build ./src -out:monkey-odin.out
alias re := repl
repl:
	just build && {{exe}} repl
ast:
	echo "AST for {{test_file}}"
	just build && {{exe}} ast {{test_dir}}{{test_file}}
bytes:
	echo "Bytecode for {{test_file}}"
	just build && {{exe}} bytes {{test_dir}}{{test_file}}
alias ta := test-all
test-all:
	odin test ./tests -all-packages
	odin test ./tests/examples 
alias c := clean
clean:
	rm ./*.out
	rm ./*.bin
alias cp := commit-push
commit-push MSG:
	git add . && git commit -m "{{MSG}}" && git push
alias tsgen := tree-sitter-gen
tree-sitter-gen:
	tree-sitter generate
alias tstest := tree-sitter-test
tree-sitter-test:
	tree-sitter parse {{tree_sitter_parse_test_file}} 

screenshot

About

monkey compiler in odin

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published