Skip to content

Commit

Permalink
Support panic and todo exprssions
Browse files Browse the repository at this point in the history
  • Loading branch information
dusty-phillips committed Aug 20, 2024
1 parent 74805b2 commit 55c35bc
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fd .gleam | entr gleam test
Are you insane?

Sweet, me too.

PRs are welcome.

### TODO
Expand All @@ -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
10 changes: 10 additions & 0 deletions src/generator.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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("(")
Expand Down
3 changes: 3 additions & 0 deletions src/python.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions src/transformer.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
65 changes: 64 additions & 1 deletion test/expression_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ pub fn negate_int_test() {
let b = -a
}"
|> macabre.compile
|> pprint.debug
|> should.be_ok
|> should.equal(
"def main():
Expand All @@ -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
Expand Down

0 comments on commit 55c35bc

Please sign in to comment.