lytlang/lib/lytlang.ex
2019-11-19 22:24:17 -06:00

81 lines
1.6 KiB
Elixir

defmodule Lytlang do
def eval(string) do
{value, _bindings_maybe} =
string
|> from_lytlang()
|> eval_elixir()
value
end
def from_lytlang(
code,
meta \\ [],
opts \\ []
)
def from_lytlang(string, _binding, opts) do
string
|> IO.inspect(label: "Lytlang Code")
|> String.to_charlist()
|> tokenize(opts)
|> parse(opts)
end
def tokenize(string, _opts \\ []) do
{:ok, tokens, _num_lines_maybe} =
:lytlang_lexer.string(string)
|> IO.inspect(label: "Tokens")
tokens
end
def parse(tokens, _opts \\ []) do
{:ok, [tree]} =
:lytlang_parser.parse(tokens)
|> IO.inspect(label: "Parsed")
tree
end
def eval_elixir(ast, opts \\ [])
def eval_elixir(ast, _opts) do
{:__block__, [], [ast]}
|> Code.eval_quoted()
|> IO.inspect()
end
end
"""
-module(elixir).
-export([eval/1, from_elixir/1, from_erlang/1]).
eval(String) ->
{value, Value, _} = erl_eval:expr(from_elixir(String), []),
Value.
% Temporary to aid debugging
from_elixir(String) ->
transform(parse(String)).
% Temporary to aid debugging
from_erlang(String) ->
{ok, Tokens, _} = erl_scan:string(String),
{ok, [Form]} = erl_parse:parse_exprs(Tokens),
Form.
parse(String) ->
{ok, Tokens, _} = elixir_lexer:string(String),
{ok, ParseTree} = elixir_parser:parse(Tokens),
ParseTree.
transform({ binary_op, Line, Op, Left, Right }) ->
{op, Line, Op, transform(Left), transform(Right)};
transform({ unary_op, Line, Op, Right }) ->
{op, Line, Op, transform(Right)};
transform({ integer, _, _ } = Expr) -> Expr.
"""