Commit 24e4aa48 authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Fix ternary operator

parent 2676022d
......@@ -1781,33 +1781,54 @@ def _conditional(cond):
basic_then = find_basic_type(cond.value['then'].exprType)
basic_else = find_basic_type(cond.value['else'].exprType)
then_len = None
if isinstance(cond.value['then'],
(ogAST.PrimSequenceOf, ogAST.PrimStringLiteral)):
then_str = array_content(cond.value['then'], then_str, basic_then)
if isinstance(cond.value['else'],
(ogAST.PrimSequenceOf, ogAST.PrimStringLiteral)):
else_str = array_content(cond.value['else'], else_str, basic_else)
if isinstance(cond.value['then'], ogAST.PrimSubstring):
if isinstance(cond.value['then'], ogAST.ExprAppend):
then_len = append_size(cond.value['then'])
stmts.append(u"tmp{idx}.Data(1..{then_len}) := {then_str};"
.format(idx=cond.value['tmpVar'],
then_len=then_len, then_str=then_str))
elif isinstance(cond.value['then'], ogAST.PrimSubstring):
stmts.append(u"tmp{idx}.Data(1..{then_str}'Length) := {then_str};"
.format(idx=cond.value['tmpVar'], then_str=then_str))
if basic_then.Min != basic_then.Max:
stmts.append(u"tmp{idx}.Length := {then_str}'Length;"
.format(idx=cond.value['tmpVar'], then_str=then_str))
then_len = u"{}'Length".format(then_str)
# stmts.append(u"tmp{idx}.Length := {then_str}'Length;"
# .format(idx=cond.value['tmpVar'], then_str=then_str))
else:
stmts.append(u'tmp{idx} := {then_str};'
.format(idx=cond.value['tmpVar'], then_str=then_str))
if then_len:
stmts.append(u"tmp{idx}.Length := {then_len};"
.format(idx=cond.value['tmpVar'], then_len=then_len))
stmts.append('else')
if isinstance(cond.value['else'], ogAST.PrimSubstring):
else_len = None
if isinstance(cond.value['else'],
(ogAST.PrimSequenceOf, ogAST.PrimStringLiteral)):
else_str = array_content(cond.value['else'], else_str, basic_else)
if isinstance(cond.value['else'], ogAST.ExprAppend):
else_len = append_size(cond.value['else'])
stmts.append(u"tmp{idx}.Data(1..{else_len}) := {else_str};"
.format(idx=cond.value['tmpVar'],
else_len=else_len, else_str=else_str))
elif isinstance(cond.value['else'], ogAST.PrimSubstring):
stmts.append(u"tmp{idx}.Data(1..{else_str}'Length) := {else_str};"
.format(idx=cond.value['tmpVar'], else_str=else_str))
if basic_else.Min != basic_else.Max:
stmts.append(u"tmp{idx}.Length := {else_str}'Length;"
.format(idx=cond.value['tmpVar'], else_str=elsn_str))
else_len = u"{}'Length".format(else_str)
# stmts.append(u"tmp{idx}.Length := {else_str}'Length;"
# .format(idx=cond.value['tmpVar'], else_str=else_str))
else:
stmts.append(u'tmp{idx} := {else_str};'.format(
idx=cond.value['tmpVar'],
else_str=else_str))
if else_len:
stmts.append(u"tmp{idx}.Length := {else_len};"
.format(idx=cond.value['tmpVar'], else_len=else_len))
stmts.append('end if;')
ada_string = u'tmp{idx}'.format(idx=cond.value['tmpVar'])
return stmts, unicode(ada_string), local_decl
......
......@@ -3954,7 +3954,7 @@ def transition(root, parent, context):
def assign(root, context):
''' Parse an assignation (a := b) in a task symbol '''
''' Parse an assignment (a := b) in a task symbol '''
errors = []
warnings = []
expr = ogAST.ExprAssign(
......@@ -4004,8 +4004,7 @@ def assign(root, context):
except Warning as warn:
warnings.append('Expression "{}": {}'
.format(expr.inputString, str(warn)))
else:
# In "else" branch because in case of exception right side may be None
if not errors:
if expr.right.exprType == UNKNOWN_TYPE or not \
isinstance(expr.right, (ogAST.ExprAppend,
ogAST.PrimSequenceOf,
......@@ -4160,7 +4159,7 @@ def task_body(root, context):
def task(root, parent=None, context=None):
''' Parse a TASK symbol (assignation or informal text) '''
''' Parse a TASK symbol (assignment or informal text) '''
errors = []
warnings = []
coord = False
......
......@@ -21,7 +21,7 @@ saved_signal;
CONNECT c AND r;
/* CIF PROCESS (225, 50), (150, 75) */
PROCESS og;
/* CIF TEXT (335, 58), (396, 136) */
/* CIF TEXT (150, 75), (396, 136) */
-- Demonstrate how to simulate the behaviour of the SAVE
-- signal. The actual SAVE implies dynamic memory allocation.
-- But using a controlled-size array and a continuous signal it
......@@ -31,59 +31,75 @@ dcl save_buffer SeqOf;
dcl param BoolType;
/* CIF ENDTEXT */
/* CIF START (0, 226), (70, 35) */
START;
/* CIF NEXTSTATE (0, 276), (70, 35) */
NEXTSTATE wait;
/* CIF STATE (291, 227), (70, 35) */
STATE wait;
/* CIF INPUT (91, 282), (84, 35) */
INPUT run;
/* CIF NEXTSTATE (79, 332), (107, 35) */
NEXTSTATE Running;
/* CIF INPUT (296, 282), (162, 35) */
INPUT saved_signal(param);
/* CIF DECISION (296, 332), (161, 50) */
/* CIF PROCEDURE (5, 163), (113, 35) */
PROCEDURE GET_PARAM;
/* CIF START (153, 81), (70, 35) */
START;
/* CIF TASK (94, 131), (188, 35) */
TASK param := save_buffer(0);
/* CIF TASK (30, 181), (316, 56) */
TASK save_buffer := if length(save_buffer) > 1
then save_buffer(1, length(save_buffer))
else {} fi;
/* CIF RETURN (170, 252), (35, 35) */
RETURN ;
ENDPROCEDURE;
/* CIF PROCEDURE (0, 106), (120, 35) */
PROCEDURE SAVE_PARAM;
/* CIF TEXT (1, 27), (289, 140) */
fpar in param BoolType;
/* CIF ENDTEXT */
/* CIF START (146, 192), (70, 28) */
START;
/* CIF DECISION (101, 238), (161, 50) */
DECISION length(save_buffer);
/* CIF ANSWER (253, 402), (60, 34) */
/* CIF ANSWER (58, 308), (60, 34) */
(<10):
/* CIF TASK (195, 451), (174, 40) */
/* CIF TASK (0, 357), (177, 50) */
TASK save_buffer :=
save_buffer // {param};
/* CIF ANSWER (457, 402), (53, 34) */
/* CIF ANSWER (262, 308), (53, 34) */
(10):
/* CIF PROCEDURECALL (382, 451), (202, 38) */
/* CIF PROCEDURECALL (187, 357), (202, 38) */
CALL writeln('Buffer Overflow!');
ENDDECISION;
/* CIF NEXTSTATE (342, 506), (70, 35) */
NEXTSTATE wait;
ENDSTATE;
/* CIF STATE (929, 221), (88, 35) */
/* CIF RETURN (164, 422), (35, 35) */
RETURN ;
ENDPROCEDURE;
/* CIF START (97, 224), (70, 35) */
START;
/* CIF NEXTSTATE (97, 274), (70, 35) */
NEXTSTATE wait;
/* CIF STATE (479, 236), (88, 35) */
STATE Running;
/* CIF INPUT (1053, 276), (70, 35) */
/* CIF INPUT (588, 291), (70, 35) */
INPUT Run;
/* CIF PROCEDURECALL (990, 326), (194, 35) */
/* CIF PROCEDURECALL (525, 341), (194, 35) */
CALL writeln('Already running');
/* CIF NEXTSTATE (1053, 376), (70, 35) */
/* CIF NEXTSTATE (588, 391), (70, 35) */
NEXTSTATE -;
/* CIF PROVIDED (718, 276), (181, 35) */
/* CIF PROVIDED (324, 291), (181, 35) */
PROVIDED length(save_buffer) > 0;
/* CIF TASK (717, 326), (182, 35) */
TASK param := save_buffer(0);
/* CIF TASK (636, 376), (344, 35) */
TASK save_buffer := save_buffer(1, length(save_buffer));
/* CIF TASK (647, 426), (323, 56) */
TASK save_buffer := if length(save_buffer) > 1
then {true} else save_buffer // {false } fi;
/* CIF TASK (650, 497), (316, 56) */
TASK save_buffer := if length(save_buffer) > 1
then save_buffer(1, length(save_buffer))
else {} fi;
/* CIF PROCEDURECALL (708, 568), (200, 35) */
/* CIF PROCEDURECALL (358, 341), (113, 35) */
CALL GET_PARAM;
/* CIF PROCEDURECALL (315, 391), (200, 35) */
CALL writeln('Running: ', param);
/* CIF NEXTSTATE (764, 618), (88, 35) */
/* CIF NEXTSTATE (371, 441), (88, 35) */
NEXTSTATE Running;
ENDSTATE;
/* CIF STATE (97, 274), (70, 35) */
STATE wait;
/* CIF INPUT (0, 329), (84, 35) */
INPUT run;
/* CIF NEXTSTATE (-2, 379), (88, 35) */
NEXTSTATE Running;
/* CIF INPUT (103, 329), (162, 35) */
INPUT saved_signal(param);
/* CIF PROCEDURECALL (100, 379), (168, 35) */
CALL SAVE_PARAM(param);
/* CIF NEXTSTATE (149, 429), (70, 35) */
NEXTSTATE wait;
ENDSTATE;
ENDPROCESS og;
ENDBLOCK;
ENDSYSTEM;
\ No newline at end of file
include ../shared.mk
all: test-ada test-llvm
edit:
$(OPENGEODE) og.pr
test-parse:
$(OPENGEODE) og.pr --check
test-ada:
$(OPENGEODE) og.pr --toAda
$(ASN1SCC) -Ada dataview.asn -typePrefix asn1Scc -equal
$(GNATMAKE) -O$(O) -c *.adb
$(GNATBIND) -n og.ali
test-c:
$(OPENGEODE) og.pr --toC
$(ASN1SCC) -c dataview.asn -typePrefix asn1Scc -equal
$(CC) -O$(O) -c *.c
test-llvm:
$(OPENGEODE) og.pr --llvm -O$(O)
$(LLC) *.ll
$(CC) -O$(O) -c *.s
coverage:
coverage run -p $(OPENGEODE) og.pr --toAda
.PHONY: all edit test-parse test-ada test-llvm coverage
TASTE-Dataview DEFINITIONS ::=
BEGIN
SeqOf ::= SEQUENCE (SIZE(0..10)) OF BoolType
BoolType ::= BOOLEAN
END
SYSTEM og;
/* CIF TEXT (159, 221), (289, 188) */
-- Text area for declarations and comments
use dv comment 'dataview.asn';
signal run;
signal saved_signal(BoolType);
signal we;
/* CIF ENDTEXT */
CHANNEL c
FROM ENV TO og WITH run,
saved_signal;
FROM og TO ENV WITH we;
ENDCHANNEL;
BLOCK og;
SIGNALROUTE r
FROM ENV TO og WITH run,
saved_signal;
FROM og TO ENV WITH we;
CONNECT c AND r;
/* CIF PROCESS (225, 50), (150, 75) */
PROCESS og;
/* CIF TEXT (335, 58), (396, 136) */
-- Various tests of the ternary operator
-- (PrimConditional)
dcl save_buffer SeqOf;
dcl param BoolType;
/* CIF ENDTEXT */
/* CIF START (0, 226), (70, 35) */
START;
/* CIF NEXTSTATE (0, 276), (70, 35) */
NEXTSTATE wait;
/* CIF STATE (291, 227), (70, 35) */
STATE wait;
/* CIF INPUT (296, 282), (162, 35) */
INPUT saved_signal(param);
/* CIF DECISION (296, 332), (161, 50) */
DECISION length(save_buffer);
/* CIF ANSWER (253, 402), (60, 34) */
(<10):
/* CIF TASK (194, 451), (177, 50) */
TASK save_buffer :=
save_buffer // {param};
/* CIF ANSWER (457, 402), (53, 34) */
(10):
/* CIF PROCEDURECALL (382, 451), (202, 38) */
CALL writeln('Buffer Overflow!');
ENDDECISION;
/* CIF NEXTSTATE (342, 516), (70, 35) */
NEXTSTATE wait;
/* CIF INPUT (91, 282), (84, 35) */
INPUT run;
/* CIF NEXTSTATE (79, 332), (107, 35) */
NEXTSTATE Running;
ENDSTATE;
/* CIF STATE (929, 221), (88, 35) */
STATE Running;
/* CIF PROVIDED (718, 276), (181, 35) */
PROVIDED length(save_buffer) > 0;
/* CIF TASK (714, 326), (188, 35) */
TASK param := save_buffer(0);
/* CIF TASK (633, 376), (350, 34) */
TASK save_buffer := save_buffer(1, length(save_buffer));
/* CIF TASK (647, 425), (323, 56) */
TASK save_buffer := if length(save_buffer) > 1
then {true} else save_buffer // {false } fi;
/* CIF TASK (650, 496), (316, 56) */
TASK save_buffer := if length(save_buffer) > 1
then save_buffer(1, length(save_buffer))
else {} fi;
/* CIF PROCEDURECALL (708, 567), (200, 35) */
CALL writeln('Running: ', param);
/* CIF NEXTSTATE (764, 617), (88, 35) */
NEXTSTATE Running;
/* CIF INPUT (1053, 276), (70, 35) */
INPUT Run;
/* CIF PROCEDURECALL (990, 326), (194, 35) */
CALL writeln('Already running');
/* CIF NEXTSTATE (1053, 376), (70, 35) */
NEXTSTATE -;
ENDSTATE;
ENDPROCESS og;
ENDBLOCK;
ENDSYSTEM;
\ No newline at end of file
Markdown is supported
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