Skip to content
Snippets Groups Projects
Commit fe715640 authored by Fabian Vu's avatar Fabian Vu
Browse files

Implement handling return parameters for external functions

parent 4a7b224a
No related branches found
No related tags found
No related merge requests found
Pipeline #148963 passed
...@@ -92,7 +92,7 @@ public class DeclarationGenerator { ...@@ -92,7 +92,7 @@ public class DeclarationGenerator {
/* /*
* This function generates code for a parameter with the given node from the AST and the information whether it is an output parameter * This function generates code for a parameter with the given node from the AST and the information whether it is an output parameter
*/ */
private String generateParameter(DeclarationNode node, boolean isReturn) { public String generateParameter(DeclarationNode node, boolean isReturn) {
ST declaration = currentGroup.getInstanceOf("parameter"); ST declaration = currentGroup.getInstanceOf("parameter");
TemplateHandler.add(declaration, "isReturn", isReturn); TemplateHandler.add(declaration, "isReturn", isReturn);
TemplateHandler.add(declaration, "type", typeGenerator.generate(node.getType())); TemplateHandler.add(declaration, "type", typeGenerator.generate(node.getType()));
......
...@@ -279,9 +279,10 @@ public class MachineGenerator implements AbstractVisitor<String, Void> { ...@@ -279,9 +279,10 @@ public class MachineGenerator implements AbstractVisitor<String, Void> {
TemplateHandler.add(machine, "externalFunctions", machineNode.getOperations() TemplateHandler.add(machine, "externalFunctions", machineNode.getOperations()
.stream() .stream()
.filter(op -> op.getName().startsWith("EXTERNAL_")) .filter(op -> op.getName().startsWith("EXTERNAL_"))
.map(op -> generateExternalFunction(op.getName(), op.getParams() .map(op -> generateExternalFunction(operationGenerator.generateReturnType(op), op.getName(), op.getParams()
.stream() .stream()
.map(DeclarationNode::getName).collect(Collectors.toList()))) .map(parameter -> declarationGenerator.generateParameter(parameter, false))
.collect(Collectors.toList())))
.collect(Collectors.toList())); .collect(Collectors.toList()));
generateBody(node, machine); generateBody(node, machine);
return machine.render(); return machine.render();
...@@ -372,8 +373,9 @@ public class MachineGenerator implements AbstractVisitor<String, Void> { ...@@ -372,8 +373,9 @@ public class MachineGenerator implements AbstractVisitor<String, Void> {
TemplateHandler.add(machine, "structs", recordStructGenerator.generateStructs()); TemplateHandler.add(machine, "structs", recordStructGenerator.generateStructs());
} }
private String generateExternalFunction(String name, List<String> parameters) { private String generateExternalFunction(String type, String name, List<String> parameters) {
ST template = currentGroup.getInstanceOf("external_function"); ST template = currentGroup.getInstanceOf("external_function");
TemplateHandler.add(template, "type", type);
TemplateHandler.add(template, "name", name); TemplateHandler.add(template, "name", name);
TemplateHandler.add(template, "parameters", parameters); TemplateHandler.add(template, "parameters", parameters);
return template.render(); return template.render();
......
...@@ -125,6 +125,7 @@ public class OperationGenerator { ...@@ -125,6 +125,7 @@ public class OperationGenerator {
TemplateHandler.add(operation, "parameters", declarationGenerator.generateDeclarations(node.getParams(), DeclarationType.PARAMETER, false)); TemplateHandler.add(operation, "parameters", declarationGenerator.generateDeclarations(node.getParams(), DeclarationType.PARAMETER, false));
TemplateHandler.add(operation, "parameterNames", node.getParams().stream().map(DeclarationNode::getName).collect(Collectors.toList())); TemplateHandler.add(operation, "parameterNames", node.getParams().stream().map(DeclarationNode::getName).collect(Collectors.toList()));
TemplateHandler.add(operation, "returnParameters", declarationGenerator.generateDeclarations(node.getOutputParams(), DeclarationType.PARAMETER, true)); TemplateHandler.add(operation, "returnParameters", declarationGenerator.generateDeclarations(node.getOutputParams(), DeclarationType.PARAMETER, true));
TemplateHandler.add(operation, "hasReturnParameters", !node.getOutputParams().isEmpty());
return operation; return operation;
} }
...@@ -142,6 +143,17 @@ public class OperationGenerator { ...@@ -142,6 +143,17 @@ public class OperationGenerator {
} }
} }
public String generateReturnType(OperationNode node) {
List<DeclarationNode> outputs = node.getOutputParams();
if(outputs.size() > 1) {
return recordStructGenerator.getStruct(node);
} else if(outputs.size() == 1) {
return typeGenerator.generate(outputs.get(0).getType());
} else {
return typeGenerator.generateVoid();
}
}
/* /*
* This function generates code for empty output parameters * This function generates code for empty output parameters
*/ */
......
...@@ -317,10 +317,14 @@ public <machine>(<if(hasExternalFunctions)>ExternalFunctionsInterface _externalF ...@@ -317,10 +317,14 @@ public <machine>(<if(hasExternalFunctions)>ExternalFunctionsInterface _externalF
method() ::= << method() ::= <<
>> >>
operation(returnType, operationName, parameters, locals, body, return, isExternal, parameterNames) ::= << operation(returnType, operationName, parameters, locals, body, return, isExternal, hasReturnParameters, parameterNames) ::= <<
public <returnType> <operationName>(<parameters; separator=", ">) { public <returnType> <operationName>(<parameters; separator=", ">) {
<if(isExternal)> <if(isExternal)>
<if(hasReturnParameters)>
return _externalFunctions.<operationName>(<parameterNames; separator=", ">);
<else>
_externalFunctions.<operationName>(<parameterNames; separator=", ">); _externalFunctions.<operationName>(<parameterNames; separator=", ">);
<endif>
<else> <else>
<locals; separator="\n"> <locals; separator="\n">
<body> <body>
...@@ -1867,6 +1871,6 @@ transition_cache_declaration(type, identifier, operationHasParams) ::= << ...@@ -1867,6 +1871,6 @@ transition_cache_declaration(type, identifier, operationHasParams) ::= <<
set_initialization(identifier, type, enums) ::= <<>> set_initialization(identifier, type, enums) ::= <<>>
external_function(name, parameters) ::= << external_function(type, name, parameters) ::= <<
void <name>(<parameters; separator=",">); <type> <name>(<parameters; separator=",">);
>> >>
\ No newline at end of file
...@@ -12,12 +12,12 @@ INITIALISATION ...@@ -12,12 +12,12 @@ INITIALISATION
cycle := 1 cycle := 1
OPERATIONS OPERATIONS
EXTERN_Init = EXTERNAL_Init =
SELECT init = FALSE THEN SELECT init = FALSE THEN
init := TRUE init := TRUE
END; END;
EXTERN_open_link = EXTERNAL_open_link =
SELECT init = TRUE THEN SELECT init = TRUE THEN
skip skip
END; END;
...@@ -28,7 +28,7 @@ OPERATIONS ...@@ -28,7 +28,7 @@ OPERATIONS
"high_level" - cflib.crazyflie.high_level_commander.HighLevelCommander "high_level" - cflib.crazyflie.high_level_commander.HighLevelCommander
"position_high_level" (the default) - cflib.positioning.position_hl_commander.PositionHlCommander "position_high_level" (the default) - cflib.positioning.position_hl_commander.PositionHlCommander
*/ */
EXTERN_open_link_with_commander_type(commander) = EXTERNAL_open_link_with_commander_type(commander) =
SELECT SELECT
init = TRUE init = TRUE
& commander : STRING & commander : STRING
...@@ -36,27 +36,27 @@ OPERATIONS ...@@ -36,27 +36,27 @@ OPERATIONS
skip skip
END; END;
EXTERN_close_link = EXTERNAL_close_link =
SELECT init = TRUE THEN SELECT init = TRUE THEN
skip skip
END; END;
EXTERN_register_sensors = EXTERNAL_register_sensors =
SELECT init = TRUE THEN SELECT init = TRUE THEN
skip skip
END; END;
EXTERN_Drone_Takeoff = EXTERNAL_Drone_Takeoff =
SELECT init = TRUE THEN SELECT init = TRUE THEN
skip skip
END; END;
EXTERN_Drone_Land = EXTERNAL_Drone_Land =
SELECT init = TRUE THEN SELECT init = TRUE THEN
skip skip
END; END;
EXTERN_Drone_Left(dist) = EXTERNAL_Drone_Left(dist) =
SELECT SELECT
init = TRUE & init = TRUE &
dist : 0..2000 dist : 0..2000
...@@ -64,7 +64,7 @@ OPERATIONS ...@@ -64,7 +64,7 @@ OPERATIONS
skip skip
END; END;
EXTERN_Drone_Right(dist) = EXTERNAL_Drone_Right(dist) =
SELECT SELECT
init = TRUE & init = TRUE &
dist : 0..2000 dist : 0..2000
...@@ -72,7 +72,7 @@ OPERATIONS ...@@ -72,7 +72,7 @@ OPERATIONS
skip skip
END; END;
EXTERN_Drone_Up(dist) = EXTERNAL_Drone_Up(dist) =
SELECT SELECT
init = TRUE & init = TRUE &
dist : 0..2000 dist : 0..2000
...@@ -80,7 +80,7 @@ OPERATIONS ...@@ -80,7 +80,7 @@ OPERATIONS
skip skip
END; END;
EXTERN_Drone_Down(dist) = EXTERNAL_Drone_Down(dist) =
SELECT SELECT
init = TRUE & init = TRUE &
dist : 0..2000 dist : 0..2000
...@@ -88,7 +88,7 @@ OPERATIONS ...@@ -88,7 +88,7 @@ OPERATIONS
skip skip
END; END;
EXTERN_Drone_Forward(dist) = EXTERNAL_Drone_Forward(dist) =
SELECT SELECT
init = TRUE & init = TRUE &
dist : 0..2000 dist : 0..2000
...@@ -96,7 +96,7 @@ OPERATIONS ...@@ -96,7 +96,7 @@ OPERATIONS
skip skip
END; END;
EXTERN_Drone_Backward(dist) = EXTERNAL_Drone_Backward(dist) =
SELECT SELECT
init = TRUE & init = TRUE &
dist : 0..2000 dist : 0..2000
...@@ -104,7 +104,7 @@ OPERATIONS ...@@ -104,7 +104,7 @@ OPERATIONS
skip skip
END; END;
Drone_Turn_Left(degrees) = EXTERNAL_Drone_Turn_Left(degrees) =
SELECT SELECT
init = TRUE & init = TRUE &
degrees : -360..360 degrees : -360..360
...@@ -112,7 +112,7 @@ OPERATIONS ...@@ -112,7 +112,7 @@ OPERATIONS
skip skip
END; END;
Drone_Turn_Right(degrees) = EXTERNAL_Drone_Turn_Right(degrees) =
SELECT SELECT
init = TRUE & init = TRUE &
degrees : -360..360 degrees : -360..360
...@@ -120,85 +120,85 @@ OPERATIONS ...@@ -120,85 +120,85 @@ OPERATIONS
skip skip
END; END;
out <-- EXTERN_Drone_GetX = out <-- EXTERNAL_Drone_GetX =
SELECT init = TRUE THEN SELECT init = TRUE THEN
out := 2000; out := 2000;
cycle := cycle + 1 cycle := cycle + 1
END; END;
out <-- EXTERN_Drone_GetY = out <-- EXTERNAL_Drone_GetY =
SELECT init = TRUE THEN SELECT init = TRUE THEN
out := 2000; out := 2000;
cycle := cycle + 1 cycle := cycle + 1
END; END;
out <-- EXTERN_Drone_GetZ = out <-- EXTERNAL_Drone_GetZ =
SELECT init = TRUE THEN SELECT init = TRUE THEN
out := 2000; out := 2000;
cycle := cycle + 1 cycle := cycle + 1
END; END;
out <-- EXTERN_Drone_GetBattery = out <-- EXTERNAL_Drone_GetBattery =
SELECT init = TRUE THEN SELECT init = TRUE THEN
out := 100; out := 100;
cycle := cycle + 1 cycle := cycle + 1
END; END;
out <-- EXTERN_Drone_GetFrontDistance = out <-- EXTERNAL_Drone_GetFrontDistance =
SELECT init = TRUE THEN SELECT init = TRUE THEN
out := 2000; out := 2000;
cycle := cycle + 1 cycle := cycle + 1
END; END;
out <-- EXTERN_Drone_GetBackDistance = out <-- EXTERNAL_Drone_GetBackDistance =
SELECT init = TRUE THEN SELECT init = TRUE THEN
out := 2000; out := 2000;
cycle := cycle + 1 cycle := cycle + 1
END; END;
out <-- EXTERN_Drone_GetUpDistance = out <-- EXTERNAL_Drone_GetUpDistance =
SELECT init = TRUE THEN SELECT init = TRUE THEN
out := 2000; out := 2000;
cycle := cycle + 1 cycle := cycle + 1
END; END;
out <-- EXTERN_Drone_GetDownDistance = out <-- EXTERNAL_Drone_GetDownDistance =
SELECT init = TRUE THEN SELECT init = TRUE THEN
out := 2000; out := 2000;
cycle := cycle + 1 cycle := cycle + 1
END; END;
out <-- EXTERN_Drone_GetLeftDistance = out <-- EXTERNAL_Drone_GetLeftDistance =
SELECT init = TRUE THEN SELECT init = TRUE THEN
out := 2000; out := 2000;
cycle := cycle + 1 cycle := cycle + 1
END; END;
out <-- EXTERN_Drone_GetRightDistance = out <-- EXTERNAL_Drone_GetRightDistance =
SELECT init = TRUE THEN SELECT init = TRUE THEN
out := 2000; out := 2000;
cycle := cycle + 1 cycle := cycle + 1
END; END;
out <-- EXTERN_Drone_GetRoll = out <-- EXTERNAL_Drone_GetRoll =
SELECT init = TRUE THEN SELECT init = TRUE THEN
out := 0; out := 0;
cycle := cycle + 1 cycle := cycle + 1
END; END;
out <-- EXTERN_Drone_GetPitch = out <-- EXTERNAL_Drone_GetPitch =
SELECT init = TRUE THEN SELECT init = TRUE THEN
out := 0; out := 0;
cycle := cycle + 1 cycle := cycle + 1
END; END;
out <-- EXTERN_Drone_GetYaw = out <-- EXTERNAL_Drone_GetYaw =
SELECT init = TRUE THEN SELECT init = TRUE THEN
out := 0; out := 0;
cycle := cycle + 1 cycle := cycle + 1
END; END;
Destroy = EXTERNAL_Destroy =
SELECT init = TRUE THEN SELECT init = TRUE THEN
init := FALSE init := FALSE
END END
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment