Skip to content
Snippets Groups Projects
Commit 66fd89ef authored by Lucas Stratmann's avatar Lucas Stratmann
Browse files

Changed Variable Handling

parent 35853868
Branches
No related tags found
No related merge requests found
......@@ -136,15 +136,27 @@ public class Translator
//added by letsoING -> simulator
//TODO check if it works as expected ;-P
//-> only changed rootTranslatorBlock.toCode to .toSim from translate
public SimCode simulate(Long blockId,Simulator sim) throws SocketNullException, SubroutineNotDeclaredException, BlockException
public SimCode simulate(Long blockId) throws SocketNullException, SubroutineNotDeclaredException, BlockException
{
TranslatorBlockFactory translatorBlockFactory = new TranslatorBlockFactory();
Block block = workspace.getEnv().getBlock(blockId);
TranslatorBlock rootTranslatorBlock = translatorBlockFactory.buildTranslatorBlock(this, blockId, block.getGenusName(), "", "", block.getBlockLabel());
rootTranslatorBlock.toSim(sim);
rootTranslatorBlock.toSim();
return null;
}
public void clearMaps() {
numberVariableSet.clear();
booleanVariableSet.clear();
}
public Map<String, String> getNuberMap() {
return numberVariableSet;
}
public Map<String, String> getBooleanMap() {
return booleanVariableSet;
}
public BlockAdaptor getBlockAdaptor()
{
return blockAdaptor;
......
......
package com.ardublock.translator.block.control;
import java.util.Map;
import java.util.Vector;
import com.ardublock.translator.Translator;
......@@ -12,8 +13,12 @@ import tec.letsgoing.ardublock.simulator.Simulator;
import tec.letsgoing.ardublock.simulator.simcode.SimCode;
import tec.letsgoing.ardublock.simulator.simcode.control.CodeWhile;
import tec.letsgoing.ardublock.simulator.simcode.datatypes.SimTypeBool;
import tec.letsgoing.ardublock.simulator.simcode.datatypes.SimTypeInt;
import tec.letsgoing.ardublock.simulator.simcode.datatypes.SimTypeString;
import tec.letsgoing.ardublock.simulator.simcode.functions.CodeExecuteFunction;
import tec.letsgoing.ardublock.simulator.simcode.functions.SimCodeFunction;
import tec.letsgoing.ardublock.simulator.simcode.vars.CodeBoolSet;
import tec.letsgoing.ardublock.simulator.simcode.vars.CodeIntSet;
public class LoopBlock extends TranslatorBlock
......@@ -40,7 +45,9 @@ public class LoopBlock extends TranslatorBlock
return ret;
}
public void toSim(Simulator sim) throws BlockException, SocketNullException {
public SimCode toSim() throws BlockException, SocketNullException {
Simulator sim = Simulator.getInstance();
translator.clearMaps();
Vector<SimCode> vec =new Vector<SimCode>();
TranslatorBlock translatorBlock = getTranslatorBlockAtSocket(0);
while (translatorBlock != null)
......@@ -50,25 +57,31 @@ public class LoopBlock extends TranslatorBlock
if (newSimCode != null) {
vec.add(newSimCode);
}
}
SimCodeFunction loop =new SimCodeFunction("loop",vec);
Vector<SimCode> mainVec = new Vector<SimCode>();
Map<String,String> numbers=translator.getNuberMap();
Map<String,String> bools=translator.getBooleanMap();
for (String var : numbers.keySet()) {
mainVec.add(new CodeIntSet(new SimTypeString(var),new SimTypeInt(0)));
}
for (String var : bools.keySet()) {
mainVec.add(new CodeBoolSet(new SimTypeString(var),new SimTypeBool(false)));
}
SimCodeFunction loop =new SimCodeFunction("loop",vec);
SimTypeBool boolTrue = new SimTypeBool(true);
Vector<SimCode> mainVec = new Vector<SimCode>();
//mainVec.add(new CodeExecuteFunction("setup"));
Vector<SimCode> loopVec = new Vector<SimCode>();
loopVec.add(new CodeExecuteFunction("loop"));
SimTypeBool boolTrue = new SimTypeBool(true);
mainVec.add(new CodeWhile(boolTrue, loopVec));
SimCodeFunction main = new SimCodeFunction("main", mainVec);
//sim.addFunctionsCode(setupCode);
System.out.println("Add new Functions");
sim.stopSimu();
sim.resetFunctions();
sim.addFunctionsCode(loop);
sim.addFunctionsCode(main);
return null;
}
}
......@@ -2,6 +2,7 @@ package com.ardublock.translator.block.control;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Vector;
......@@ -19,8 +20,12 @@ import tec.letsgoing.ardublock.simulator.Simulator;
import tec.letsgoing.ardublock.simulator.simcode.SimCode;
import tec.letsgoing.ardublock.simulator.simcode.control.CodeWhile;
import tec.letsgoing.ardublock.simulator.simcode.datatypes.SimTypeBool;
import tec.letsgoing.ardublock.simulator.simcode.datatypes.SimTypeInt;
import tec.letsgoing.ardublock.simulator.simcode.datatypes.SimTypeString;
import tec.letsgoing.ardublock.simulator.simcode.functions.CodeExecuteFunction;
import tec.letsgoing.ardublock.simulator.simcode.functions.SimCodeFunction;
import tec.letsgoing.ardublock.simulator.simcode.vars.CodeBoolSet;
import tec.letsgoing.ardublock.simulator.simcode.vars.CodeIntSet;
@SuppressWarnings("unused")
......@@ -96,7 +101,9 @@ public class SketchBlock extends TranslatorBlock
}
}
public void toSim(Simulator sim) throws BlockException, SocketNullException {
public SimCode toSim() throws BlockException, SocketNullException {
Simulator sim = Simulator.getInstance();
translator.clearMaps();
Vector<SimCode> mainVec =new Vector<SimCode>();
Vector<SimCode> setup =new Vector<SimCode>();
Vector<SimCode> loop =new Vector<SimCode>();
......@@ -110,6 +117,16 @@ public class SketchBlock extends TranslatorBlock
mainVec.add(newSimCode);
}
}
Map<String,String> numbers=translator.getNuberMap();
Map<String,String> bools=translator.getBooleanMap();
for (String var : numbers.keySet()) {
mainVec.add(new CodeIntSet(new SimTypeString(var),new SimTypeInt(0)));
}
for (String var : bools.keySet()) {
mainVec.add(new CodeBoolSet(new SimTypeString(var),new SimTypeBool(false)));
}
//Setup
translatorBlock = getTranslatorBlockAtSocket(1);
while (translatorBlock != null)
......@@ -150,7 +167,7 @@ public class SketchBlock extends TranslatorBlock
sim.addFunctionsCode(setupFunc);
sim.addFunctionsCode(loopFunc);
sim.addFunctionsCode(main);
return null;
}
......
......
......@@ -3,6 +3,11 @@ package com.ardublock.translator.block.numbers;
import com.ardublock.translator.Translator;
import com.ardublock.translator.block.TranslatorBlock;
import tec.letsgoing.ardublock.simulator.simcode.SimCode;
import tec.letsgoing.ardublock.simulator.simcode.datatypes.SimTypeString;
import tec.letsgoing.ardublock.simulator.simcode.vars.CodeBoolGet;
import tec.letsgoing.ardublock.simulator.simcode.vars.CodeIntGet;
public class LocalVariableDigitalBlock extends TranslatorBlock
{
public LocalVariableDigitalBlock(Long blockId, Translator translator, String codePrefix, String codeSuffix, String label)
......@@ -25,4 +30,13 @@ public class LocalVariableDigitalBlock extends TranslatorBlock
}
return codePrefix + newInternalName + codeSuffix;
}
public String toString() {
return label;
}
public SimCode toSim() {
return new CodeBoolGet(new SimTypeString(label));
}
}
......@@ -78,6 +78,9 @@ public class SetterVariableDigitalBlock extends TranslatorBlock
public SimCode toSim() throws SocketNullException {
TranslatorBlock translatorBlock = this.getRequiredTranslatorBlockAtSocket(0);
SimTypeString variableName = new SimTypeString(translatorBlock.toString());
if (translatorBlock instanceof VariableDigitalBlock) {
translator.addBooleanVariable(translatorBlock.toString(), translatorBlock.toString());
}
translatorBlock = this.getRequiredTranslatorBlockAtSocket(1);
SimTypeBool value = new SimTypeBool(translatorBlock.toSim());
return new CodeBoolSet(variableName,value);
......
......
......@@ -74,8 +74,12 @@ public class SetterVariableNumberBlock extends TranslatorBlock
public SimCode toSim() throws SocketNullException {
TranslatorBlock translatorBlock = this.getRequiredTranslatorBlockAtSocket(0);
SimTypeString variableName = new SimTypeString(translatorBlock.toString());
if (translatorBlock instanceof VariableNumberBlock) {
translator.addNumberVariable(translatorBlock.toString(), translatorBlock.toString());
}
translatorBlock = this.getRequiredTranslatorBlockAtSocket(1);
SimTypeInt value = new SimTypeInt(translatorBlock.toSim());
return new CodeIntSet(variableName,value);
}
}
......@@ -211,7 +211,7 @@ public class SimulateCodeButtonListener implements ActionListener
translator.setRootBlockName("loop");
Block loopBlock = renderableBlock.getBlock();
code.append(translator.translate(loopBlock.getBlockID()));
translator.simulate(loopBlock.getBlockID(),sim);
translator.simulate(loopBlock.getBlockID());
}
for (RenderableBlock renderableBlock : subroutineBlockSet)
......@@ -219,7 +219,7 @@ public class SimulateCodeButtonListener implements ActionListener
translator.setRootBlockName("subroutine");
Block subroutineBlock = renderableBlock.getBlock();
code.append(translator.translate(subroutineBlock.getBlockID()));
translator.simulate(subroutineBlock.getBlockID(),sim);
translator.simulate(subroutineBlock.getBlockID());
//collect simcode objects here -> all SimCodeObjects inside main subroutines
//eg. simList.append(translator.simulate(subroutineBlock.getBlockID())); -> simList = simCodeObject-List
}
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment