Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
feat compiler compile the core words
Browse files Browse the repository at this point in the history
  • Loading branch information
Ljzn committed Aug 29, 2020
1 parent f638d12 commit 9ee5b95
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
3 changes: 3 additions & 0 deletions c/core/core.fth
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,6 @@

: ^ ( a b -- c )
2dup | ~ tas & fas | ~ ;

: checksigverify ( sig pubkey -- bool )
checksig verify ;
6 changes: 6 additions & 0 deletions c/lib/mini_forth/c.ex
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,10 @@ defmodule MiniForth.C do
end

defp expand_main(v, _), do: v

@core_string File.read!("core/core.fth")

def interpret_core_word(op) do
@core_string |> parse() |> replace() |> Map.get(op) || [op]
end
end
5 changes: 4 additions & 1 deletion c/src/compiler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ quote(L) ->
{quote, L}.

execute_latest_quote([{quote, L} | M], R) ->
lists:reverse(R) ++ [interpreter:simple_eval(L) | M];
L1 = ['Elixir.MiniForth.C':interpret_core_word(Op) || Op <- L],
L2 = lists:flatten(L1),
% io:format("~p~n", [L2]),
lists:reverse(R) ++ [interpreter:simple_eval(L2) | M];
execute_latest_quote([H | M], R) ->
execute_latest_quote(M, [H | R]);
execute_latest_quote([], R) ->
Expand Down
4 changes: 3 additions & 1 deletion c/src/expander.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,6 @@ s([], R, _) ->
lists:reverse(R).

eval(Op, M, A) ->
interpreter:eval([Op], M, A).
Ops = 'Elixir.MiniForth.C':interpret_core_word(Op),
% io:format("~p~n", [Ops]),
interpreter:eval(Ops, M, A).
10 changes: 5 additions & 5 deletions c/src/interpreter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ eval(['if' | C], [H | M], A) ->
{T, F, R} = branches(C),
eval(choose(H, T, F) ++ R, M, A);
eval([H | C], M, A) ->
% io:format("~p~n", [{H, C, M}]),
'Elixir.MiniForth.U':debug({H, C, M}, []),
M1 = op(H, M), eval(C, M1, A).

op(0, M) -> [<<>> | M];
Expand Down Expand Up @@ -70,9 +70,9 @@ op('=verify', [Y, X | _M]) ->
raise_error("equal_verify failed.\nleft: ~p, right: "
"~p~n",
[X, Y]);
op(checksignverify, M) ->
% TODO
M;
op(checksig, [_Y, _X | M]) ->
% TODO always return true now
[<<1>> | M];
op(checkmultisignverify, M) ->
% TODO
M;
Expand Down Expand Up @@ -160,7 +160,7 @@ num2bin(N) ->

num2bin(_, 0) ->
raise_error('SCRIPT_ERR_IMPOSSIBLE_ENCODING');
num2bin(N, S) -> B = num2bin(N), pad_zeros(B, S).
num2bin(N, S) -> B = num2bin(N), S1 = bin2num(S), pad_zeros(B, S1).

pad_zeros(<<>>, S) ->
<<0:(8*S)>>;
Expand Down

0 comments on commit 9ee5b95

Please sign in to comment.