Commit 54e91fe2 authored by dbarbera's avatar dbarbera
Browse files

Added ternary operator generation

parent 0d7880da
......@@ -994,7 +994,27 @@ def _mantissa_base_exp(primary):
@expression.register(ogAST.PrimIfThenElse)
def _if_then_else(ifthen):
''' Generate the code for ternary operator '''
raise NotImplementedError
func = g.builder.basic_block.function
if_block = func.append_basic_block('ternary:if')
else_block = func.append_basic_block('ternary:else')
end_block = func.append_basic_block('')
res_ptr = g.builder.alloca(generate_type(ifthen.exprType))
cond_val = expression(ifthen.value['if'])
g.builder.cbranch(cond_val, if_block, else_block)
g.builder.position_at_end(if_block)
generate_assign(res_ptr, expression(ifthen.value['then']))
g.builder.branch(end_block)
g.builder.position_at_end(else_block)
generate_assign(res_ptr, expression(ifthen.value['else']))
g.builder.branch(end_block)
g.builder.position_at_end(end_block)
return g.builder.load(res_ptr)
@expression.register(ogAST.PrimSequence)
......
......@@ -23,6 +23,14 @@ DCL varlist VarIntList;
TASK varlist := {1, 2, 3, 4, 5, 6, 7};
/* CIF NEXTSTATE (0, 250), (70, 35) */
NEXTSTATE Wait;
/* CIF LABEL (470, 571), (78, 35) */
CONNECTION label1:
/* CIF PROCEDURECALL (438, 621), (141, 35) */
CALL writeln('label1');
/* CIF JOIN (491, 671), (35, 35) */
JOIN label2;
/* CIF End Label */
ENDCONNECTION;
/* CIF LABEL (490, 793), (75, 35) */
CONNECTION label2:
/* CIF PROCEDURECALL (459, 843), (136, 35) */
......@@ -35,14 +43,6 @@ DCL varlist VarIntList;
NEXTSTATE wait;
/* CIF End Label */
ENDCONNECTION;
/* CIF LABEL (470, 571), (78, 35) */
CONNECTION label1:
/* CIF PROCEDURECALL (438, 621), (141, 35) */
CALL writeln('label1');
/* CIF JOIN (491, 671), (35, 35) */
JOIN label2;
/* CIF End Label */
ENDCONNECTION;
/* CIF STATE (0, 250), (70, 35) */
STATE Wait;
/* CIF INPUT (0, 305), (70, 35) */
......@@ -60,77 +60,85 @@ DCL varlist VarIntList;
ENDDECISION;
/* CIF DECISION (0, 514), (70, 50) */
DECISION n;
/* CIF ANSWER (-11, 459), (70, 24) */
/* CIF ANSWER (-11, 454), (70, 24) */
(2):
/* CIF PROCEDURECALL (-47, 498), (142, 35) */
/* CIF PROCEDURECALL (-47, 493), (142, 35) */
CALL writeln('ERROR');
/* CIF ANSWER (-163, 460), (70, 24) */
/* CIF ANSWER (-163, 455), (70, 24) */
(1):
/* CIF PROCEDURECALL (-199, 499), (142, 35) */
/* CIF PROCEDURECALL (-199, 494), (142, 35) */
CALL writeln('ERROR');
/* CIF ANSWER (132, 459), (70, 24) */
/* CIF ANSWER (132, 454), (70, 24) */
else:
/* CIF PROCEDURECALL (105, 498), (124, 35) */
/* CIF PROCEDURECALL (105, 493), (124, 35) */
CALL writeln('OK');
ENDDECISION;
/* CIF TASK (0, 549), (70, 35) */
/* CIF TASK (0, 544), (70, 35) */
TASK n := 7;
/* CIF DECISION (0, 599), (70, 50) */
/* CIF DECISION (0, 594), (70, 50) */
DECISION n;
/* CIF ANSWER (-159, 599), (70, 24) */
/* CIF ANSWER (-159, 589), (70, 24) */
(2):
/* CIF PROCEDURECALL (-197, 638), (147, 35) */
/* CIF PROCEDURECALL (-197, 628), (147, 35) */
CALL writeln('ERROR');
/* CIF ANSWER (-14, 599), (70, 24) */
/* CIF ANSWER (-14, 589), (70, 24) */
(<10):
/* CIF PROCEDURECALL (-40, 638), (123, 35) */
/* CIF PROCEDURECALL (-40, 628), (123, 35) */
CALL writeln('OK');
/* CIF ANSWER (128, 609), (70, 24) */
/* CIF ANSWER (128, 599), (70, 24) */
else:
/* CIF PROCEDURECALL (91, 648), (142, 35) */
/* CIF PROCEDURECALL (91, 638), (142, 35) */
CALL writeln('ERROR');
ENDDECISION;
/* CIF TASK (-48, 698), (167, 56) */
/* CIF TASK (-48, 688), (167, 56) */
TASK for x in range(3):
call writeln(x);
endfor;
/* CIF TASK (0, 769), (70, 35) */
/* CIF TASK (0, 759), (70, 35) */
TASK n := 0;
/* CIF TASK (-55, 819), (181, 56) */
/* CIF TASK (-55, 809), (181, 56) */
TASK for x in range(2, 10, 2):
n := (n + x) mod 255
endfor;
/* CIF PROCEDURECALL (-15, 890), (100, 34) */
/* CIF PROCEDURECALL (-15, 880), (100, 34) */
CALL writeln(n)
/* CIF COMMENT (105, 779), (70, 35) */
/* CIF COMMENT (105, 764), (70, 35) */
COMMENT '20';
/* CIF TASK (-36, 939), (142, 56) */
/* CIF TASK (-36, 929), (142, 56) */
TASK for x in l1:
call writeln(x);
endfor;
/* CIF TASK (-38, 1010), (146, 56) */
/* CIF TASK (-38, 1000), (146, 56) */
TASK for p in l2:
call writeln(p!x);
endfor;
/* CIF TASK (-36, 1081), (142, 56) */
/* CIF TASK (-36, 1071), (142, 56) */
TASK for x in varlist:
call writeln(x);
endfor;
/* CIF TASK (-26, 1152), (123, 35) */
/* CIF TASK (-26, 1142), (123, 35) */
TASK varlist := {7, 8};
/* CIF TASK (-36, 1202), (142, 56) */
/* CIF TASK (-36, 1192), (142, 56) */
TASK for x in varlist:
call writeln(x);
endfor;
/* CIF PROCEDURECALL (-38, 1273), (146, 35) */
/* CIF PROCEDURECALL (-38, 1263), (146, 35) */
CALL writeln(varlist(0));
/* CIF PROCEDURECALL (-38, 1323), (146, 35) */
/* CIF PROCEDURECALL (-38, 1313), (146, 35) */
CALL writeln(varlist(1));
/* CIF PROCEDURECALL (-46, 1373), (162, 35) */
/* CIF PROCEDURECALL (-46, 1363), (162, 35) */
CALL writeln(99 in varlist);
/* CIF PROCEDURECALL (-42, 1423), (154, 35) */
/* CIF PROCEDURECALL (-42, 1413), (154, 35) */
CALL writeln(8 in varlist);
/* CIF JOIN (17, 1473), (35, 35) */
/* CIF TASK (0, 1463), (70, 35) */
TASK n := 1;
/* CIF TASK (-65, 1513), (200, 35) */
TASK n := if n = 0 then 2 else 4 fi;
/* CIF PROCEDURECALL (-79, 1563), (228, 35) */
CALL writeln(n)
/* CIF COMMENT (169, 1563), (70, 35) */
COMMENT '4';
/* CIF JOIN (17, 1613), (35, 35) */
JOIN label1;
ENDSTATE;
ENDPROCESS controlflow;
\ No newline at end of file
......@@ -22,6 +22,7 @@ OK
8
FALSE
TRUE
4
label1
label2
label3
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment