Skip to content

Commit

Permalink
chore: Formatting + DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
Olian04 committed Jul 23, 2024
1 parent 6f802d5 commit 36f0c84
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 163 deletions.
50 changes: 35 additions & 15 deletions src/handles/internal/ctx_utils.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ pub fn get_property(
get(path, root_ctx, index)
|> result.try(fn(it) {
case it {
ctx.Str(value) -> value |> Ok
ctx.Int(value) -> value |> int.to_string |> Ok
ctx.Float(value) -> value |> float.to_string |> Ok
ctx.Str(value) -> Ok(value)
ctx.Int(value) ->
value
|> int.to_string
|> Ok
ctx.Float(value) ->
value
|> float.to_string
|> Ok
ctx.List(_) ->
error.UnexpectedType(index, path, "List", ["Str", "Int", "Float"])
|> Error
Expand All @@ -75,15 +81,22 @@ pub fn get_list(
get(path, root_ctx, index)
|> result.try(fn(it) {
case it {
ctx.List(value) -> value |> Ok
ctx.Str(_) -> error.UnexpectedType(index, path, "Str", ["List"]) |> Error
ctx.Int(_) -> error.UnexpectedType(index, path, "Int", ["List"]) |> Error
ctx.List(value) -> Ok(value)
ctx.Str(_) ->
error.UnexpectedType(index, path, "Str", ["List"])
|> Error
ctx.Int(_) ->
error.UnexpectedType(index, path, "Int", ["List"])
|> Error
ctx.Bool(_) ->
error.UnexpectedType(index, path, "Bool", ["List"]) |> Error
error.UnexpectedType(index, path, "Bool", ["List"])
|> Error
ctx.Float(_) ->
error.UnexpectedType(index, path, "Float", ["List"]) |> Error
error.UnexpectedType(index, path, "Float", ["List"])
|> Error
ctx.Dict(_) ->
error.UnexpectedType(index, path, "Dict", ["List"]) |> Error
error.UnexpectedType(index, path, "Dict", ["List"])
|> Error
}
})
}
Expand All @@ -96,15 +109,22 @@ pub fn get_bool(
get(path, root_ctx, index)
|> result.try(fn(it) {
case it {
ctx.Bool(value) -> value |> Ok
ctx.Bool(value) -> Ok(value)
ctx.List(_) ->
error.UnexpectedType(index, path, "List", ["Bool"]) |> Error
ctx.Str(_) -> error.UnexpectedType(index, path, "Str", ["Bool"]) |> Error
ctx.Int(_) -> error.UnexpectedType(index, path, "Int", ["Bool"]) |> Error
error.UnexpectedType(index, path, "List", ["Bool"])
|> Error
ctx.Str(_) ->
error.UnexpectedType(index, path, "Str", ["Bool"])
|> Error
ctx.Int(_) ->
error.UnexpectedType(index, path, "Int", ["Bool"])
|> Error
ctx.Float(_) ->
error.UnexpectedType(index, path, "Float", ["Bool"]) |> Error
error.UnexpectedType(index, path, "Float", ["Bool"])
|> Error
ctx.Dict(_) ->
error.UnexpectedType(index, path, "Dict", ["Bool"]) |> Error
error.UnexpectedType(index, path, "Dict", ["Bool"])
|> Error
}
})
}
79 changes: 29 additions & 50 deletions src/handles/internal/engine.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,32 @@ fn eval(
eval(rest_action, new_ctx, partials, builder)
[RunAst([]), ..rest_action] -> eval(rest_action, ctx, partials, builder)
[RunAst([parser.Constant(_, value), ..rest_ast]), ..rest_action] ->
eval(
[RunAst(rest_ast), ..rest_action],
ctx,
partials,
string_builder.append(builder, value),
)
[RunAst(rest_ast), ..rest_action]
|> eval(ctx, partials, string_builder.append(builder, value))
[RunAst([parser.Property(index, path), ..rest_ast]), ..rest_action] ->
case ctx_utils.get_property(path, ctx, index) {
Error(err) -> Error(err)
Ok(value) ->
eval(
[RunAst(rest_ast), ..rest_action],
ctx,
partials,
string_builder.append(builder, value),
)
[RunAst(rest_ast), ..rest_action]
|> eval(ctx, partials, string_builder.append(builder, value))
}
[RunAst([parser.Partial(index, id, path), ..rest_ast]), ..rest_action] ->
case dict.get(partials, id) {
Error(_) -> Error(error.UnknownPartial(index, id))
Error(_) ->
error.UnknownPartial(index, id)
|> Error
Ok(partial_ast) ->
case ctx_utils.get(path, ctx, index) {
Error(err) -> Error(err)
Ok(inner_ctx) ->
eval(
[
SetCtx(inner_ctx),
RunAst(partial_ast),
SetCtx(ctx),
RunAst(rest_ast),
..rest_action
],
ctx,
partials,
builder,
)
[
SetCtx(inner_ctx),
RunAst(partial_ast),
SetCtx(ctx),
RunAst(rest_ast),
..rest_action
]
|> eval(ctx, partials, builder)
}
}
[
Expand All @@ -72,14 +62,11 @@ fn eval(
case ctx_utils.get_bool(path, ctx, start_index) {
Error(err) -> Error(err)
Ok(False) ->
eval([RunAst(rest_ast), ..rest_action], ctx, partials, builder)
[RunAst(rest_ast), ..rest_action]
|> eval(ctx, partials, builder)
Ok(True) ->
eval(
[RunAst(children), RunAst(rest_ast), ..rest_action],
ctx,
partials,
builder,
)
[RunAst(children), RunAst(rest_ast), ..rest_action]
|> eval(ctx, partials, builder)
}
[
RunAst([
Expand All @@ -91,14 +78,11 @@ fn eval(
case ctx_utils.get_bool(path, ctx, start_index) {
Error(err) -> Error(err)
Ok(True) ->
eval([RunAst(rest_ast), ..rest_action], ctx, partials, builder)
[RunAst(rest_ast), ..rest_action]
|> eval(ctx, partials, builder)
Ok(False) ->
eval(
[RunAst(children), RunAst(rest_ast), ..rest_action],
ctx,
partials,
builder,
)
[RunAst(children), RunAst(rest_ast), ..rest_action]
|> eval(ctx, partials, builder)
}
[
RunAst([
Expand All @@ -110,18 +94,13 @@ fn eval(
case ctx_utils.get_list(path, ctx, start_index) {
Error(err) -> Error(err)
Ok([]) ->
eval([RunAst(rest_ast), ..rest_action], ctx, partials, builder)
[RunAst(rest_ast), ..rest_action]
|> eval(ctx, partials, builder)
Ok(ctxs) ->
eval(
ctxs
|> list.flat_map(fn(new_ctx) {
[SetCtx(new_ctx), RunAst(children), SetCtx(ctx)]
})
|> list.append([RunAst(rest_ast), ..rest_action]),
ctx,
partials,
builder,
)
ctxs
|> list.flat_map(fn(new_ctx) { [SetCtx(new_ctx), RunAst(children)] })
|> list.append([SetCtx(ctx), RunAst(rest_ast), ..rest_action])
|> eval(ctx, partials, builder)
}
}
}
Expand Down
43 changes: 29 additions & 14 deletions src/handles/internal/parser.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,64 @@ pub type AST {

type ParseResult {
EOF(List(AST))
BlockEnd(index: Int, block.Kind, List(AST), List(tokenizer.Token))
BlockEnd(
index: Int,
kind: block.Kind,
children: List(AST),
rest: List(tokenizer.Token),
)
}

fn parse(
tokens: List(tokenizer.Token),
ast: List(AST),
tokens: List(tokenizer.Token),
) -> Result(ParseResult, error.TokenizerError) {
case tokens {
[] -> Ok(EOF(list.reverse(ast)))
[tokenizer.BlockEnd(index, kind), ..tail] ->
Ok(BlockEnd(index, kind, list.reverse(ast), tail))

[tokenizer.Constant(index, value), ..tail] ->
parse(tail, [Constant(index, value), ..ast])
[Constant(index, value), ..ast]
|> parse(tail)
[tokenizer.Property(index, path), ..tail] ->
parse(tail, [Property(index, path), ..ast])
[Property(index, path), ..ast]
|> parse(tail)
[tokenizer.Partial(index, id, value), ..tail] ->
parse(tail, [Partial(index, id, value), ..ast])
[Partial(index, id, value), ..ast]
|> parse(tail)

[tokenizer.BlockStart(start_index, start_kind, path), ..tail] ->
case parse(tail, []) {
case parse([], tail) {
Error(err) -> Error(err)
Ok(EOF(_)) -> Error(error.UnbalancedBlock(start_index))
Ok(EOF(_)) ->
start_index
|> error.UnbalancedBlock
|> Error
Ok(BlockEnd(end_index, end_kind, children, rest))
if end_kind == start_kind
->
parse(rest, [
Block(start_index, end_index, start_kind, path, children),
..ast
])
Ok(BlockEnd(index, _, _, _)) -> Error(error.UnexpectedBlockEnd(index))
[Block(start_index, end_index, start_kind, path, children), ..ast]
|> parse(rest)
Ok(BlockEnd(index, _, _, _)) ->
index
|> error.UnexpectedBlockEnd
|> Error
}
}
}

pub fn run(
tokens: List(tokenizer.Token),
) -> Result(List(AST), error.TokenizerError) {
parse(tokens, [])
parse([], tokens)
|> result.try(fn(it) {
case it {
EOF(ast) -> Ok(ast)
BlockEnd(index, _, _, _) -> Error(error.UnexpectedBlockEnd(index))
BlockEnd(index, _, _, _) ->
index
|> error.UnexpectedBlockEnd
|> Error
}
})
}
Loading

0 comments on commit 36f0c84

Please sign in to comment.