diff --git a/lib/lytlang.xrl b/lib/lytlang.xrl new file mode 100644 index 0000000..673d07e --- /dev/null +++ b/lib/lytlang.xrl @@ -0,0 +1,12 @@ +%% number +[0-9]+ : {token, + { number, + TokenLine, + list_to_integer(TokenChars) + } + }. +%% left paren +\( : {token, {'(', TokenLine}}. +%% ... other tokens +%% skip white spaces +[\s\n\r\t]+ : skip_token. diff --git a/lib/transpiler.ex b/lib/transpiler.ex index b83acbc..7806d4a 100644 --- a/lib/transpiler.ex +++ b/lib/transpiler.ex @@ -56,10 +56,10 @@ defmodule Lytlang.Transpiler do state = case line_tokens do ["!mod", module_name | rest] -> - %{state | ast: [{:module, String.to_atom(module_name), []} | ast]} + %{state | ast: [{:module, module_name, []} | ast]} ["fn", fn_name | rest] -> - %{state | ast: [{:function, String.to_atom(fn_name), []} | ast]} + %{state | ast: [{:function, fn_name, []} | ast]} expr -> expr |> IO.inspect(label: "lyt expression") @@ -77,8 +77,8 @@ defmodule Lytlang.Transpiler do case leaf do :global -> child.({"", ""}, state) - {:module, mod, []} -> child.({"defmodule #{to_string(mod)} do", "end"}, state) - {:fn, fname, []} -> child.({"def #{to_string(fname)} do", "end"}, state) + {:module, mod, []} -> child.({"defmodule #{mod} do", "end"}, state) + {:fn, fname, []} -> child.({"def #{fname} do", "end"}, state) expr when is_binary(expr) -> [expr] end end @@ -88,6 +88,6 @@ defmodule Lytlang.Transpiler do end def tokenize(s) do - Regex.split(~r/\s/, s, true) + Regex.split(~r/\s/, s) end end diff --git a/test/fixtures/example.lyt b/test/fixtures/example.lyt index cca7230..f7e4338 100644 --- a/test/fixtures/example.lyt +++ b/test/fixtures/example.lyt @@ -1,5 +1,11 @@ !mod Leetcode +# Look, a comment! + +### part of the comment +Look, a multiline comment! +part of the comment ### + @doc LeetCode #1: two_sum @@ -30,7 +36,9 @@ fn longest_non_repeating_substring s s |> String.codepoints |> Enum.reduce {0 0 0 %{}} &ln3_reducer/2 |> elem 0 fn ln3_new_least least map char - Map.has_key? map char ? max least ((Map.get map char) + 1) : least + Map.has_key? map char :: + true -> max least ((Map.get map char) + 1) + _ -> least fn ln3_reducer char {mx i least map} least = ln3_new_least least map char diff --git a/test/fixtures/example_projects/hello_world/hello_world.lyt b/test/fixtures/example_projects/hello_world/hello_world.lyt new file mode 100644 index 0000000..b0d5558 --- /dev/null +++ b/test/fixtures/example_projects/hello_world/hello_world.lyt @@ -0,0 +1 @@ +"Hello, World!" diff --git a/test/fixtures/example_projects/simple_app/app.lyt b/test/fixtures/example_projects/simple_app/app.lyt new file mode 100644 index 0000000..392acbc --- /dev/null +++ b/test/fixtures/example_projects/simple_app/app.lyt @@ -0,0 +1,24 @@ +mod SimpleApp +entry + :project + deps: + {:cowboy, "~> 2.6"} + {:plug, "~> 1.8"} + {:plug_cowboy, "~> 2.0"} + :app + [extra_applications: [:logger]] + +import Plug.Conn + +fn init default_options + IO.puts "initializing plug" + default_options + +fn call conn:Plug.Conn.t() _options = [] + IO.puts "calling plug" + + conn + |> put_resp_content_type "text/plain" + |> send_resp 200 "Hello world" + +# in lytx run `Plug.Adapters.Cowboy.http SimpleApp []`