Skip to content

Commit

Permalink
wrote some libraries for the new powerful macro system
Browse files Browse the repository at this point in the history
  • Loading branch information
zack-bitcoin committed Feb 10, 2017
1 parent 4b1b574 commit 3fcaec4
Show file tree
Hide file tree
Showing 7 changed files with 468 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/opcodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ opcode | symbol | stack changes | comment
132 | nil | -- [] | this is the root of a list.
134 | ++ | X Y -- Z | appends 2 lists or 2 binaries. Cannot append a list to a binary. Also works on pairs of lists.
135 | split | N Binary -- BinaryA BinaryB | Binary A has N*8 many bits. BinaryA appended to BinaryB makes Binary.
136 | reverse | F -- G | only works on lists
136 | reverse | F -- G | only works on lists
137 | is_list | L -- L B | checks if the thing on the top of the stack is a list or not. Does not drop it.


The following are compiler macros that make it easier to program:
Expand Down
14 changes: 14 additions & 0 deletions examples/cond_macro.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(macro = (A B)
'(nop A B === swap drop swap drop))
(macro cond (X)%this cond is a macro. it generates byte code.
(cond (((= X ()) '(nop))%this cond is part of the pre-processor. It cannot generate byte code.
(true '(nop
`(car (car X))
if
`(car (cdr (car X)))
else
(cond `(cdr X))
then)))))
(cond (((= 4 5) 3)
(false 2)
(true 1)))
11 changes: 11 additions & 0 deletions examples/first_macro.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(macro = (A B)
(quote nop A B === swap drop swap drop))
(macro twos (X)
(cond (((= X 0) '(nil))
(true '(cons 2 (twos (- X 1)))))))
(macro things (X Y)
(cond (((= X 0) '(nil))
(true '(cons Y (things (- X 1) Y))))))


(= (things 3 5) (cons 5 (cons 5 (cons 5 nil))))
23 changes: 23 additions & 0 deletions examples/square_each_macro.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(macro square (X) (* X X))
(macro = (A B)
'(nop A B === swap drop swap drop))


(macro sqr (X)
(cond (((= X ()) '(nil))
((is_list X) '(cons (square `(car X))
(sqr `(cdr X))))
(true X))))
(macro tree (X)
(cond (((= X ()) '(nil))
((not (is_list X)) X)
((is_list (car X)) '(cons `(tree (car X))
`(tree (cdr X))))
(true '(cons `(car X)
`(tree (cdr X)))))))

(and
(= (sqr (2 3))
(cons 4 (cons 9 nil)))
(= (tree (4 9))
(cons 4 (cons 9 nil))))
10 changes: 10 additions & 0 deletions src/chalang.erl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ new_state(TotalCoins, Height, Slash, Oracle, Accounts, Channels) ->
-define(append, 134).
-define(split, 135).
-define(reverse, 136).
-define(is_list, 137).
-define(int_bits, 32).

%op_gas limits our program in time.
Expand Down Expand Up @@ -496,6 +497,15 @@ run3(?reverse, D) ->
[H|T] = D#d.stack,
D#d{op_gas = D#d.op_gas - length(H),
stack = [lists:reverse(H)|T]};
run3(?is_list, D) ->
[H|T] = D#d.stack,
G = if
is_list(H) -> <<1:?int_bits>>;
true -> <<0:?int_bits>>
end,
D#d{op_gas = D#d.op_gas - 1,
stack = [G|[H|T]],
ram_current = D#d.ram_current - 1};
run3(?nop, D) -> D.


Expand Down
1 change: 1 addition & 0 deletions src/compiler_chalang.erl
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ w2o(<<"nil">>) -> 132;
w2o(<<"++">>) -> 134;
w2o(<<"split">>) -> 135;
w2o(<<"reverse">>) -> 136;
w2o(<<"is_list">>) -> 137;
w2o(_) -> not_op.
%to_opcodes([<<"or_die">>|R], F, Out) ->
%( bool -- )
Expand Down
Loading

0 comments on commit 3fcaec4

Please sign in to comment.