diff --git a/README.md b/README.md index b2c2f43..2f9270b 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,10 @@ PRs are welcome. ### TODO - flesh out this list +- custom types with no fields are probably not working +- custom types with unlabelled fields are probably not working - most expressions aren't implemented yet +- haven't really tested with nesting of expressions - need to print out nice errors when glance fails to parse - No List or Result custom types yet - glance doesn't support comments diff --git a/src/generator.gleam b/src/generator.gleam index 5d431c1..6cd7149 100644 --- a/src/generator.gleam +++ b/src/generator.gleam @@ -79,6 +79,10 @@ fn generate_expression(expression: python.Expression) { |> string_builder.append("[") |> string_builder.append(index |> int.to_string) |> string_builder.append("]") + python.FieldAccess(expression, label) -> + generate_expression(expression) + |> string_builder.append(".") + |> string_builder.append(label) python.Call(function_name, arguments) -> string_builder.new() |> string_builder.append(function_name) diff --git a/src/python.gleam b/src/python.gleam index 9600642..ed05a7c 100644 --- a/src/python.gleam +++ b/src/python.gleam @@ -38,6 +38,7 @@ pub type Expression { Panic(Expression) Todo(Expression) TupleIndex(tuple: Expression, index: Int) + FieldAccess(container: Expression, label: String) 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 34b07a6..af992e9 100644 --- a/src/transformer.gleam +++ b/src/transformer.gleam @@ -123,6 +123,9 @@ fn transform_expression(expression: glance.Expression) -> python.Expression { glance.TupleIndex(tuple, index) -> python.TupleIndex(transform_expression(tuple), index) + glance.FieldAccess(container: expression, label:) -> + python.FieldAccess(transform_expression(expression), label) + glance.BinaryOperator(glance.Pipe, left, glance.Variable(function)) -> { // simple pipe left |> foo python.Call(function, [transform_expression(left)]) diff --git a/test/expression_test.gleam b/test/expression_test.gleam index 0fbb7fa..552050e 100644 --- a/test/expression_test.gleam +++ b/test/expression_test.gleam @@ -184,6 +184,19 @@ pub fn tuple_index_test() { ) } +pub fn field_access_test() { + "fn main() { + foo.b + }" + |> macabre.compile + |> should.be_ok + |> should.equal( + "def main(): + foo.b + ", + ) +} + pub fn binop_int_add_test() { "fn main() { 40 + 2