Skip to content

Commit

Permalink
Fix handling of binary ops with casting (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
kshyatt-aws authored Nov 22, 2024
1 parent 9869d0b commit 1fa1764
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,18 @@ function parse_function_expr(expr_head, arguments, tokens, stack, start, qasm)
end
end

function parse_cast_expr(expr_head, tokens, stack, start, qasm)
interior = extract_expression(tokens, lparen, rparen, stack, start, qasm)
raw_expr = QasmExpression(:cast, expr_head, parse_expression(interior, stack, start, qasm))
if !isempty(tokens) && first(tokens)[end] == operator
next_op_token = parse_identifier(popfirst!(tokens), qasm)
right_hand_side = parse_expression(tokens, stack, start, qasm)::QasmExpression
return QasmExpression(:binary_op, Symbol(next_op_token.args[1]), raw_expr, right_hand_side)
else
return raw_expr
end
end

function parse_expression(tokens::Vector{Tuple{Int64, Int32, Token}}, stack, start, qasm)
expr_head, start_token = expression_start(tokens, stack, start, qasm)
start_token_type = start_token[end]
Expand All @@ -508,8 +520,7 @@ function parse_expression(tokens::Vector{Tuple{Int64, Int32, Token}}, stack, sta
elseif start_token_type (frame_token, waveform_token) && (next_token[end] (lbracket, identifier))
expr = QasmExpression(:classical_declaration, expr_head, parse_expression(tokens, stack, start, qasm))
elseif start_token_type == classical_type && next_token[end] == lparen
interior = extract_expression(tokens, lparen, rparen, stack, start, qasm)
expr = QasmExpression(:cast, expr_head, parse_expression(interior, stack, start, qasm))
expr = parse_cast_expr(expr_head, tokens, stack, start, qasm)
elseif next_token[end] == assignment
expr = parse_classical_assignment(expr_head, tokens, stack, start, qasm)
elseif next_token[end] == operator
Expand Down
13 changes: 8 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,10 @@ Quasar.builtin_gates[] = complex_builtin_gates
("float a = 2*(3 - 4)*5;", -10),
("int a = (3 - 4)*5;", -5),
("int a = (3-4)*(5+2);", -7),
("int a = int(2.0) + int(3.0);", 5),
("float a = 1/2+4;", 4.5),
("float a = 2**int(2.0);", 4.0),
("int a = int(2**2);", 4),
("int a = 2 + 3*4 - 5;", 14 - 5),
("complex[float] a = -5+4im;", -5+4im),
("complex[float] a = -(5+4im);", -5-4im),
Expand Down Expand Up @@ -347,11 +350,11 @@ Quasar.builtin_gates[] = complex_builtin_gates
end
@testset "Casting" begin
@testset "Casting to $to_type from $from_type" for (to_type, to_value) in (("bool", true),), (from_type, from_value) in (("int[32]", "32",),
("uint[16]", "1",),
("float", "2.5",),
("bool", "true",),
("bit", "\"1\"",),
)
("uint[16]", "1",),
("float", "2.5",),
("bool", "true",),
("bit", "\"1\"",),
)
qasm = """
$from_type a = $from_value;
$to_type b = $to_type(a);
Expand Down

0 comments on commit 1fa1764

Please sign in to comment.