Skip to content

Commit

Permalink
Hook up field access
Browse files Browse the repository at this point in the history
  • Loading branch information
dusty-phillips committed Aug 20, 2024
1 parent af3b22d commit 0657cc5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/generator.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/python.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions src/transformer.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
Expand Down
13 changes: 13 additions & 0 deletions test/expression_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0657cc5

Please sign in to comment.