From 55c35bcc1a335e18c10496b6dcee3c1f266f8aef Mon Sep 17 00:00:00 2001 From: Dusty Phillips Date: Tue, 20 Aug 2024 11:01:09 -0300 Subject: [PATCH] Support panic and todo exprssions --- README.md | 2 ++ src/generator.gleam | 10 ++++++ src/python.gleam | 3 ++ src/transformer.gleam | 10 ++++++ test/expression_test.gleam | 65 +++++++++++++++++++++++++++++++++++++- 5 files changed, 89 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ccc6107..98aa411 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ fd .gleam | entr gleam test Are you insane? Sweet, me too. + PRs are welcome. ### TODO @@ -56,3 +57,4 @@ PRs are welcome. - no concept of a "project", gleam.toml, downloading dependencies - only compiles one module at a time - eliminate all todos in source code +- panic should probably raise a custom/stdlib exception diff --git a/src/generator.gleam b/src/generator.gleam index 0cc19bd..5d431c1 100644 --- a/src/generator.gleam +++ b/src/generator.gleam @@ -57,6 +57,16 @@ fn generate_expression(expression: python.Expression) { python.Variable(value) -> string_builder.from_string(value) python.Negate(expression) -> generate_expression(expression) |> string_builder.prepend("-") + python.Not(expression) -> + generate_expression(expression) |> string_builder.prepend("not ") + python.Panic(expression) -> + generate_expression(expression) + |> string_builder.prepend("raise BaseException(") + |> string_builder.append(")") + python.Todo(expression) -> + generate_expression(expression) + |> string_builder.prepend("raise NotImplementedError(") + |> string_builder.append(")") python.Tuple(expressions) -> string_builder.new() |> string_builder.append("(") diff --git a/src/python.gleam b/src/python.gleam index dda46c0..9600642 100644 --- a/src/python.gleam +++ b/src/python.gleam @@ -34,6 +34,9 @@ pub type Expression { Variable(String) Tuple(List(Expression)) Negate(Expression) + Not(Expression) + Panic(Expression) + Todo(Expression) TupleIndex(tuple: Expression, index: Int) Call(function_name: String, arguments: List(Expression)) BinaryOperator(name: BinaryOperator, left: Expression, right: Expression) diff --git a/src/transformer.gleam b/src/transformer.gleam index 1a4d624..8f65850 100644 --- a/src/transformer.gleam +++ b/src/transformer.gleam @@ -89,6 +89,16 @@ fn transform_expression(expression: glance.Expression) -> python.Expression { glance.Variable(string) -> python.Variable(string) glance.NegateInt(expression) -> python.Negate(transform_expression(expression)) + glance.Panic(option.None) -> + python.Panic(python.String("panic expression evaluated")) + glance.Panic(option.Some(expression)) -> + python.Panic(transform_expression(expression)) + glance.Todo(option.None) -> + python.Todo(python.String("This has not yet been implemented")) + glance.Todo(option.Some(expression)) -> + python.Todo(transform_expression(expression)) + glance.NegateBool(expression) -> + python.Not(transform_expression(expression)) glance.Call(glance.Variable(function_name), arguments) -> { python.Call( function_name, diff --git a/test/expression_test.gleam b/test/expression_test.gleam index 7318e4f..0fbb7fa 100644 --- a/test/expression_test.gleam +++ b/test/expression_test.gleam @@ -99,7 +99,6 @@ pub fn negate_int_test() { let b = -a }" |> macabre.compile - |> pprint.debug |> should.be_ok |> should.equal( "def main(): @@ -108,6 +107,70 @@ pub fn negate_int_test() { ) } +pub fn negate_bool_test() { + "fn main() { + let b = !True + }" + |> macabre.compile + |> should.be_ok + |> should.equal( + "def main(): + b = not True", + ) +} + +pub fn empty_panic_test() { + "fn main() { + panic + }" + |> macabre.compile + |> should.be_ok + |> should.equal( + "def main(): + raise BaseException(\"panic expression evaluated\") + ", + ) +} + +pub fn string_panic_test() { + "fn main() { + panic as \"my custom panic\" + }" + |> macabre.compile + |> should.be_ok + |> should.equal( + "def main(): + raise BaseException(\"my custom panic\") + ", + ) +} + +pub fn empty_todo_test() { + "fn main() { + todo + }" + |> macabre.compile + |> should.be_ok + |> should.equal( + "def main(): + raise NotImplementedError(\"This has not yet been implemented\") + ", + ) +} + +pub fn string_todo_test() { + "fn main() { + todo as \"much is yet to be done\" + }" + |> macabre.compile + |> should.be_ok + |> should.equal( + "def main(): + raise NotImplementedError(\"much is yet to be done\") + ", + ) +} + pub fn tuple_index_test() { "fn main() { #(42, 12.5, \"foo\").1