Skip to content

Commit

Permalink
Merge pull request #1127 from fesch/bugfix
Browse files Browse the repository at this point in the history
Version 3.32-16 candidate
  • Loading branch information
fesch authored Feb 28, 2024
2 parents 2276c96 + 713520b commit fc93d6c
Show file tree
Hide file tree
Showing 19 changed files with 1,405 additions and 275 deletions.
36 changes: 36 additions & 0 deletions Translator.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: Author: Kay Gürtzig
::
:: Description: Structorizer Translator start script for Windows
::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: Revision List
::
:: Author Date Description
:: ------ ---- -----------
:: Kay Gürtzig 2024-01-26 First Issue (derived from Arranger.bat)
::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: Check for the correct Java version
@echo off
setlocal
set REQVERSION=11
for /f "delims=" %%a in (' java -version ^2^>^&^1 ^| find "version" ') do set JAVAVER=%%a
set JAVAVER=%JAVAVER:"=_%
for /f "tokens=2 delims=_" %%a in ("%JAVAVER%") do set JAVAVER=%%a
for /f "tokens=1,2 delims=." %%a in ("%JAVAVER%") do (
set VERSION=%%a
if %%VERSION%% equ 1 set VERSION=%%b
)
if %VERSION% lss %REQVERSION% (
echo on
echo "Your Java version is %VERSION%, but version %REQVERSION% is required. Please update."
@goto :exit
)
:: Actual start (Java version is fine)
echo on
java -cp "%~d0%~p0Structorizer.app\Contents\Java\Structorizer.jar" lu.fisch.structorizer.locales.Translator -test
:exit
4 changes: 2 additions & 2 deletions buildapp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
name="Structorizer"
displayname="Structorizer"
identifier="lu.fisch.Structorizer"
shortversion="3.32-07"
version="3.32-07"
shortversion="3.32-16"
version="3.32-16"
icon="icons/Structorizer.icns"
mainclassname="Structorizer"
copyright="Bob Fisch"
Expand Down
6 changes: 5 additions & 1 deletion src/lu/fisch/structorizer/elements/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
* Kay Gürtzig 2022-08-20 Enh. #1066: New static method retrieveComponentNames()
* Kay Gürtzig 2022-08-22 Bugfix #1068: Type inference failure for array initialisers mended
* Kay Gürtzig 2023-12-14 Issue #1119: To set an empty string as text now leads to an empty StringList
* Kay Gürtzig 2024-01-22 Bugfix #1125: Equality check must consider disabled state
*
******************************************************************************************************
*
Expand Down Expand Up @@ -305,7 +306,7 @@ public String toString()
public static final long E_HELP_FILE_SIZE = 12300000;
public static final String E_DOWNLOAD_PAGE = "https://www.fisch.lu/Php/download.php";
// END KGU#791 2020-01-20
public static final String E_VERSION = "3.32-15";
public static final String E_VERSION = "3.32-16";
public static final String E_THANKS =
"Developed and maintained by\n"+
" - Robert Fisch <[email protected]>\n"+
Expand Down Expand Up @@ -1021,6 +1022,9 @@ public boolean equals(Element _another)
// START KGU#156 2016-03-12: Colour had to be disabled due to races
//if (isEqual) isEqual = this.getColor().equals(_another.getColor());
// END KGU#156 2016-03-12
// START KGU#1113 2024-01-22: Enh. #270, Bugfix #1125 disabled state is a difference
if (isEqual) isEqual = this.disabled == _another.disabled;
// END KGU#1113 2024-01-22
return isEqual;
}
// END KGU#119 2016-01-02
Expand Down
80 changes: 78 additions & 2 deletions src/lu/fisch/structorizer/generators/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
* Kay Gürtzig 2023-10-17 Bugfix #1099: Constants defined by an external routine call no longer moved
* to top (to change execution order could severely compromise the algorithm!)
* Kay Gürtzig 2023-12-14 Bugfix #1118: The comment of Instructions without a line wasn't exported
* Kay Gürtzig 2023-12-26 Issue #1123: Translation of built-in function random() added.
*
******************************************************************************************************
*
Expand Down Expand Up @@ -368,6 +369,14 @@ protected boolean arrayBracketsAtTypeName()
return false;
}
// END KGU#784 2019-12-02

// START KGU#1112 2023-12-26: Issue #1123: Care for random translation
/** @return whether the translation of a {@code random(expr)} expression requires a local Random class instance. */
protected boolean needsRandomClassInstance()
{
return false;
}
// END KGU#1112 2023-12-26

// START KGU#18/KGU#23 2015-11-01 Transformation decomposed
/* (non-Javadoc)
Expand Down Expand Up @@ -571,6 +580,20 @@ protected String transformTokens(StringList tokens)
tokens.set(pos, "(char)");
}
// END KGU#150 2016-04-03
// START KGU#1112 2023-12-17: Issue #1123: Convert random(expr) calls
pos = -1;
while ((pos = tokens.indexOf("random", pos+1)) >= 0 && pos+2 < tokens.count() && tokens.get(pos+1).equals("("))
{
StringList exprs = Element.splitExpressionList(tokens.subSequence(pos+2, tokens.count()),
",", true);
if (exprs.count() == 2 && exprs.get(1).startsWith(")")) {
tokens.remove(pos, tokens.count());
tokens.add(Element.splitLexically("(rand() % (" + exprs.get(0) + ")" + exprs.get(1), true));
pos += 5;
}
}
// END KGU#1112 2023-12-17

// START KGU#311 2016-12-22: Enh. #314 - Structorizer file API support
// KGU#832 2020-03-23: Bugfix #840 Even in case of disabled File API elements the code should be transformed
//if (this.usesFileAPI) {
Expand Down Expand Up @@ -2712,11 +2735,13 @@ protected String generatePreamble(Root _root, String _indent, StringList varName

// START KGU#376 2017-09-26: Enh #389 - declaration stuff condensed to a method
/**
* Appends constant, type, and variable definitions for the passed-in {@link Root} {@code _root}.<br/>
* Appends constant, type, and variable definitions for the passed-in {@link Root} {@code _root}.
*
* @param _root - the diagram the declarations and definitions of are to be added
* @param _indent - the proper indentation as String
* @param _varNames - optionally the StringList of the variable names to be declared (my be null)
* @param _force - true means that the addition is forced even if option {@link #isInternalDeclarationAllowed()} is set
* @param _force - {@code true} means that the addition is forced even if some adversary option like
* e.g. {@link #isInternalDeclarationAllowed()} is set
*/
protected void appendDefinitions(Root _root, String _indent, StringList _varNames, boolean _force) {
// TODO: structured constants must be defined after the type definitions (see PasGenerator)!
Expand Down Expand Up @@ -2771,6 +2796,19 @@ protected void appendDefinitions(Root _root, String _indent, StringList _varName
}
}
// END KGU#376 2017-09-28
// START KGU#1112 2023-12-26: Issue #1123 Support for built-in random function
if (needsRandomClassInstance() && rootCallsRandom(_root)) {
String rndInst = "randGen";
//if (_varNames.contains(rndInst)) {
// int i = 0;
// while (_varNames.contains(rndInst + i)) {
// i++;
// }
// rndInst += i;
//}
addCode("Random " + rndInst + " = new Random();", _indent, false);
}
// END KGU#1112 2023-12-26
if (code.count() > lastLine) {
addSepaLine();
}
Expand All @@ -2780,6 +2818,44 @@ protected void appendDefinitions(Root _root, String _indent, StringList _varName
}
// END KGU#852 2020-04-22
}

/**
* Checks whether the given {@code _root} (i.e. its substructure) contains
* a {@code random(..)} call.
*
* @param _root - the diagram to search
* @return {@code true} if some of the elements of {@code _root} refers to
* function {@code random()}
*/
private boolean rootCallsRandom(Root _root) {
final class RandomFinder implements IElementVisitor {
public boolean callsRandom = false;

@Override
public boolean visitPreOrder(Element _ele) {
StringList elText = _ele.getUnbrokenText();
for (int i = 0; i < elText.count(); i++) {
StringList tokens = Element.splitLexically(elText.get(i), true);
tokens.removeAll(" ");
int posRnd = tokens.indexOf("random");
if (posRnd >= 0 && posRnd+1 < tokens.count() && tokens.get(posRnd + 1).equals("(")) {
callsRandom = true;
return false;
}
}
return true;
}

@Override
public boolean visitPostOrder(Element _ele) {
return true;
}
};

RandomFinder randFinder = new RandomFinder();
_root.traverse(randFinder);
return randFinder.callsRandom;
}
// END KGU#376 2017-09-26

// START KGU#375 2017-04-12: Enh. #388 common preparation of constants and variables
Expand Down
22 changes: 22 additions & 0 deletions src/lu/fisch/structorizer/generators/CSharpGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
* Bugfix #1092: Approach for more sensible export of type aliases
* Kay Gürtzig 2023-10-04 Bugfix #1093 Undue final return 0 on function diagrams
* Kay Gürtzig 2023-10-15 Bugfix #1096 Initialisation for multidimensional arrays fixed
* Kay Gürtzig 2023-12-27 Issue #1123: Translation of built-in function random() added.
*
******************************************************************************************************
*
Expand Down Expand Up @@ -254,6 +255,14 @@ protected boolean allowsMixedModule()
return true;
}

// START KGU#1112 2023-12-27: Issue #1123: Care for random translation
@Override
protected boolean needsRandomClassInstance()
{
return true;
}
// END KGU#1112 2023-12-27

/* (non-Javadoc)
* @see lu.fisch.structorizer.generators.CGenerator#insertPrototype(lu.fisch.structorizer.elements.Root, java.lang.String, boolean, int)
*/
Expand Down Expand Up @@ -440,6 +449,19 @@ protected boolean isInternalDeclarationAllowed()
protected String transformTokens(StringList tokens)
{
tokens.replaceAll("Infinity", "double.PositiveInfinity");
// START KGU#1112 2023-12-17: Issue #1123: Convert random(expr) calls
int pos = -1;
while ((pos = tokens.indexOf("random", pos+1)) >= 0 && pos+2 < tokens.count() && tokens.get(pos+1).equals("("))
{
StringList exprs = Element.splitExpressionList(tokens.subSequence(pos+2, tokens.count()),
",", true);
if (exprs.count() == 2 && exprs.get(1).startsWith(")")) {
tokens.remove(pos, tokens.count());
tokens.add(Element.splitLexically("randGen.Next(" + exprs.get(0) + exprs.get(1), true));
pos += 3;
}
}
// END KGU#1112 2023-12-17
return super.transformTokens(tokens);
}
// END KGU#920 2021-02-03
Expand Down
5 changes: 3 additions & 2 deletions src/lu/fisch/structorizer/generators/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3672,12 +3672,13 @@ protected void appendGlobalDefinitions(Root _root, String _indent, boolean _forc
}

/**
* Appends constant, type, and variable definitions for the passed-in {@link Root} {@code _root}
* Appends constant, type, and variable definitions for the passed-in {@link Root} {@code _root}.
*
* @param _root - the diagram the declarations and definitions of which are to be inserted
* @param _indent - the proper indentation as String
* @param _varNames - optionally the StringList of the variable names to be declared (my be null)
* @param _force - true means that the insertion is forced even if some adversary option like e.g.
* {@link CGenerator#isInternalDeclarationAllowed()} is set
* {@link CGenerator#isInternalDeclarationAllowed()} is set
*/
protected void appendDefinitions(Root _root, String _indent, StringList _varNames, boolean _force) {
// To be overridden by subclasses
Expand Down
49 changes: 47 additions & 2 deletions src/lu/fisch/structorizer/generators/JavaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@
* Kay Gürtzig 2021-10-03 Bugfix #993: Wrong handling of constant parameters
* Kay Gürtzig 2021-12-05 Bugfix #1024: Precautions against defective record initializers
* Kay Gürtzig 2023-09-28 Bugfix #1092: Type alias export flaws mended, at least as comment
* Kay Gürtzig 2023-10-04 Bugfix #1093 Undue final return 0 on function diagrams
* Kay Gürtzig 2023-10-15 Bugfix #1096 Initialisation for multidimensional arrays fixed
* Kay Gürtzig 2023-10-04 Bugfix #1093: Undue final return 0 on function diagrams
* Kay Gürtzig 2023-10-15 Bugfix #1096: Initialisation for multidimensional arrays fixed
* Kay Gürtzig 2023-12-25 Issue #1121: Scanner method should be type-specific where possible
* Kay Gürtzig 2023-12-27 Issue #1123: Translation of built-in function random() added.
*
******************************************************************************************************
*
Expand Down Expand Up @@ -133,6 +135,7 @@
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.Scanner;

import lu.fisch.diagrcontrol.DiagramController;
import lu.fisch.structorizer.elements.*;
Expand Down Expand Up @@ -264,6 +267,11 @@ protected String getIncludePattern()

/************ Code Generation **************/

// START KGU#1109 2023-12-25: Issue #1121 Better type-specific input support
/** List of Java types with specific {@code next...()} method on class {@link Scanner}. */
static private final StringList SCANNER_TYPES = StringList.explode("double,float,long,int,short,boolean,byte", ",");
// END KGU#1109 2023-12-23

// START KGU#542 2019-11-18: Enh. #739 - we need the current root for token transformation
/** Currently exported {@link Root} object */
protected Root root = null;
Expand Down Expand Up @@ -305,6 +313,14 @@ protected boolean arrayBracketsAtTypeName()
return true;
}

// START KGU#1112 2023-12-27: Issue #1123: Care for random translation
@Override
protected boolean needsRandomClassInstance()
{
return true;
}
// END KGU#1112 2023-12-27

// START KGU#480 2018-01-21: Enh. #490 Improved support for Turtleizer export
/**
* Maps light-weight instances of DiagramControllers for API retrieval
Expand Down Expand Up @@ -440,6 +456,19 @@ else if (this.root != null && (constVal = this.root.constants.get(token)) != nul
// END KGU#542 2019-11-18
}
}
// START KGU#1112 2023-12-17: Issue #1123: Convert random(expr) calls
int pos = -1;
while ((pos = tokens.indexOf("random", pos+1)) >= 0 && pos+2 < tokens.count() && tokens.get(pos+1).equals("("))
{
StringList exprs = Element.splitExpressionList(tokens.subSequence(pos+2, tokens.count()),
",", true);
if (exprs.count() == 2 && exprs.get(1).startsWith(")")) {
tokens.remove(pos, tokens.count());
tokens.add(Element.splitLexically("(randGen.nextInt() % (" + exprs.get(0) + ")" + exprs.get(1), true));
pos += 7;
}
}
// END KGU#1112 2023-12-17
return super.transformTokens(tokens);
}
// END KGU#446 2017-10-27
Expand Down Expand Up @@ -499,6 +528,22 @@ protected String transform(String _input, boolean _doInputOutput)
s = s.substring(0, s.length() - inpRepl.length()-1) + s.substring(s.length() - inpRepl.length()+2);
}
//END KGU#281
// START KGU#1109 2023-12-25: Issue #1121 Try a more type-specific input
else {
int posRepl = s.indexOf(inpRepl);
if (posRepl > 0) {
int posSemi = s.lastIndexOf(";", posRepl);
String target = s.substring(posSemi + 1, posRepl).trim();
if (this.varNames.contains(target) && this.typeMap.containsKey(target)) {
String typename = this.transformType(typeMap.get(target).getCanonicalType(true, true), "???");
if (SCANNER_TYPES.contains(typename)) {
s = s.replace("nextLine()",
"next" + Character.toUpperCase(typename.charAt(0)) + typename.substring(1) + "()");
}
}
}
}
// END KGU#1109 2023-12-25
}

// Math function
Expand Down
24 changes: 21 additions & 3 deletions src/lu/fisch/structorizer/generators/JsGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
* Kay Gürtzig 2020-04-22 Bugfix #854: Deterministic topological order of type definitions ensured
* Kay Gürtzig 2021-02-03 Issue #920: Transformation for "Infinity" literal, see comment
* Kay Gürtzig 2021-12-05 Bugfix #1024: Precautions against defective record initializers
* Kay Gürtzig 2023-10-04 Bugfix #1093 Undue final return 0 on function diagrams
* Kay Gürtzig 2023-10-16 Bugfix #1098 Recursive application of initializer transformation ensured
* Kay Gürtzig 2023-10-04 Bugfix #1093: Undue final return 0 on function diagrams
* Kay Gürtzig 2023-10-16 Bugfix #1098: Recursive application of initializer transformation ensured
* Kay Gürtzig 2023-12-26 Bugfix #1122: getInputReplacer() was defective for promptless input.
* Kay Gürtzig 2023-12-27 Issue #1123: Translation of built-in function random() added.
*
******************************************************************************************************
*
Expand Down Expand Up @@ -150,7 +152,9 @@ protected String getInputReplacer(boolean withPrompt) {
if (withPrompt) {
return "$2 = prompt($1)";
}
return "$1 = prompt(String($1))";
// START KGU#1110 2023-12-26: Issue #1122 prompt requires two arguments
return "$1 = prompt(\"$1\")";
// END KGU#1110 2023-12-26
}

/* (non-Javadoc)
Expand Down Expand Up @@ -235,6 +239,20 @@ else if (posBrace == 0) {
}
}
// END KGU#1091 2023-10-16
// START KGU#1112 2023-12-17: Issue #1123: Convert random(expr) calls
int pos = -1;
while ((pos = tokens.indexOf("random", pos+1)) >= 0 && pos+2 < tokens.count() && tokens.get(pos+1).equals("("))
{
StringList exprs = Element.splitExpressionList(tokens.subSequence(pos+2, tokens.count()),
",", true);
if (exprs.count() == 2 && exprs.get(1).startsWith(")")) {
tokens.remove(pos, tokens.count());
// The code "§RANDOM§" is to protect it from super. It will be replaced after super call.
tokens.add(Element.splitLexically("Math.floor(Math.random() * " + exprs.get(0) + exprs.get(1), true));
pos += 9;
}
}
// END KGU#1112 2023-12-17
return super.transformTokens(tokens);
}

Expand Down
Loading

0 comments on commit fc93d6c

Please sign in to comment.