Begin transition to leex and yecc?
This commit is contained in:
parent
f09f9d8edf
commit
4a0708ad07
5 changed files with 51 additions and 6 deletions
12
lib/lytlang.xrl
Normal file
12
lib/lytlang.xrl
Normal file
|
@ -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.
|
|
@ -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
|
||||
|
|
10
test/fixtures/example.lyt
vendored
10
test/fixtures/example.lyt
vendored
|
@ -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
|
||||
|
|
1
test/fixtures/example_projects/hello_world/hello_world.lyt
vendored
Normal file
1
test/fixtures/example_projects/hello_world/hello_world.lyt
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
"Hello, World!"
|
24
test/fixtures/example_projects/simple_app/app.lyt
vendored
Normal file
24
test/fixtures/example_projects/simple_app/app.lyt
vendored
Normal file
|
@ -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 []`
|
Loading…
Reference in a new issue