lytlang/lib/lytlang.ex

82 lines
1.6 KiB
Elixir
Raw Normal View History

2019-05-11 13:01:39 -05:00
defmodule Lytlang do
2019-11-15 00:51:27 -06:00
def eval(string) do
2019-11-19 22:24:17 -06:00
{value, _bindings_maybe} =
2019-11-15 00:51:27 -06:00
string
|> from_lytlang()
2019-11-19 22:24:17 -06:00
|> eval_elixir()
2019-05-11 13:01:39 -05:00
2019-11-15 00:51:27 -06:00
value
end
2019-05-11 13:01:39 -05:00
2019-11-19 21:11:28 -06:00
def from_lytlang(
code,
2019-11-19 22:24:17 -06:00
meta \\ [],
2019-11-19 21:11:28 -06:00
opts \\ []
)
2019-11-19 22:24:17 -06:00
def from_lytlang(string, _binding, opts) do
2019-11-15 11:19:38 -06:00
string
2019-11-19 22:24:17 -06:00
|> IO.inspect(label: "Lytlang Code")
2019-11-15 11:19:38 -06:00
|> String.to_charlist()
2019-11-19 22:24:17 -06:00
|> tokenize(opts)
|> parse(opts)
2019-11-15 00:51:27 -06:00
end
2019-05-11 13:01:39 -05:00
2019-11-19 22:24:17 -06:00
def tokenize(string, _opts \\ []) do
{:ok, tokens, _num_lines_maybe} =
2019-11-15 00:51:27 -06:00
:lytlang_lexer.string(string)
2019-11-15 11:19:38 -06:00
|> IO.inspect(label: "Tokens")
2019-05-11 13:01:39 -05:00
2019-11-15 00:51:27 -06:00
tokens
2019-05-11 13:01:39 -05:00
end
2019-11-19 22:24:17 -06:00
def parse(tokens, _opts \\ []) do
{:ok, [tree]} =
2019-11-15 00:51:27 -06:00
:lytlang_parser.parse(tokens)
2019-11-15 11:19:38 -06:00
|> IO.inspect(label: "Parsed")
2019-05-11 14:00:46 -05:00
2019-11-15 00:51:27 -06:00
tree
end
2019-05-11 14:00:46 -05:00
2019-11-19 22:24:17 -06:00
def eval_elixir(ast, opts \\ [])
2019-11-15 11:19:38 -06:00
2019-11-19 22:24:17 -06:00
def eval_elixir(ast, _opts) do
{:__block__, [], [ast]}
|> Code.eval_quoted()
|> IO.inspect()
2019-11-15 11:19:38 -06:00
end
2019-05-11 13:01:39 -05:00
end
2019-11-15 00:51:27 -06:00
"""
-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.
"""