Skip to content

Commit

Permalink
Simple Assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
dusty-phillips committed Aug 19, 2024
1 parent 09b69d1 commit ebf3b60
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/generator.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn generate_expression(expression: python.Expression) {
python.String(string) -> string_builder.from_strings(["\"", string, "\""])
python.Number(number) -> string_builder.from_string(number)
python.Bool(value) -> string_builder.from_string(value)
python.Variable(value) -> string_builder.from_string(value)
python.Tuple(expressions) ->
string_builder.new()
|> string_builder.append("(")
Expand Down Expand Up @@ -86,6 +87,12 @@ fn generate_statement(statement: python.Statement) -> StringBuilder {
python.Expression(expression) ->
generate_expression(expression)
|> generator_helpers.append_if_not_empty("\n")
python.SimpleAssignment(name, value) -> {
string_builder.new()
|> string_builder.append(name)
|> string_builder.append(" = ")
|> string_builder.append_builder(generate_expression(value))
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/python.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub type Expression {
String(String)
Number(String)
Bool(String)
Variable(String)
Tuple(List(Expression))
TupleIndex(tuple: Expression, index: Int)
Call(function_name: String, arguments: List(Expression))
Expand All @@ -39,6 +40,7 @@ pub type Expression {

pub type Statement {
Expression(Expression)
SimpleAssignment(name: String, value: Expression)
}

pub type FunctionParameter {
Expand Down
14 changes: 13 additions & 1 deletion src/transformer.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ fn transform_expression(expression: glance.Expression) -> python.Expression {
glance.Int(string) | glance.Float(string) -> python.Number(string)
glance.Variable("True") -> python.Bool("True")
glance.Variable("False") -> python.Bool("False")
glance.Variable(string) -> python.Variable(string)
glance.Tuple(expressions) ->
expressions
|> list.map(transform_expression)
Expand Down Expand Up @@ -139,7 +140,18 @@ fn transform_statement(statement: glance.Statement) -> python.Statement {
glance.Expression(expression) -> {
python.Expression(transform_expression(expression))
}
_ -> todo as "not all statements are defined yet"
glance.Assignment(
kind: glance.Let,
pattern: glance.PatternVariable(variable),
value: value,
..,
) -> python.SimpleAssignment(variable, transform_expression(value))
glance.Assignment(..) ->
todo as "Non-trivial assignments are not supported yet"
_ -> {
pprint.debug(statement)
todo as "not all statements are defined yet"
}
}
}

Expand Down

0 comments on commit ebf3b60

Please sign in to comment.