From 9ee5b953ee1f8022b3045c7db71675b6c22009be Mon Sep 17 00:00:00 2001 From: zhanglinjie Date: Sat, 29 Aug 2020 13:58:14 +0800 Subject: [PATCH] feat compiler compile the core words --- c/core/core.fth | 3 +++ c/lib/mini_forth/c.ex | 6 ++++++ c/src/compiler.erl | 5 ++++- c/src/expander.erl | 4 +++- c/src/interpreter.erl | 10 +++++----- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/c/core/core.fth b/c/core/core.fth index 80f6fcb..353838c 100644 --- a/c/core/core.fth +++ b/c/core/core.fth @@ -67,3 +67,6 @@ : ^ ( a b -- c ) 2dup | ~ tas & fas | ~ ; + +: checksigverify ( sig pubkey -- bool ) + checksig verify ; \ No newline at end of file diff --git a/c/lib/mini_forth/c.ex b/c/lib/mini_forth/c.ex index 8c0f886..4cb78ce 100644 --- a/c/lib/mini_forth/c.ex +++ b/c/lib/mini_forth/c.ex @@ -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 diff --git a/c/src/compiler.erl b/c/src/compiler.erl index 31690df..49848ad 100644 --- a/c/src/compiler.erl +++ b/c/src/compiler.erl @@ -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) -> diff --git a/c/src/expander.erl b/c/src/expander.erl index 9de7602..70cb097 100644 --- a/c/src/expander.erl +++ b/c/src/expander.erl @@ -67,4 +67,6 @@ s([], R, _) -> lists:reverse(R). eval(Op, M, A) -> - interpreter:eval([Op], M, A). \ No newline at end of file + Ops = 'Elixir.MiniForth.C':interpret_core_word(Op), + % io:format("~p~n", [Ops]), + interpreter:eval(Ops, M, A). \ No newline at end of file diff --git a/c/src/interpreter.erl b/c/src/interpreter.erl index af82b4c..91b015a 100644 --- a/c/src/interpreter.erl +++ b/c/src/interpreter.erl @@ -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]; @@ -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; @@ -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)>>;