removed the newList() function and added a list literal

This commit is contained in:
hneemann 2021-03-13 21:21:06 +01:00
parent 82c1ac1190
commit 39ae6ead0a
6 changed files with 31 additions and 11 deletions

View File

@ -326,6 +326,6 @@ The following functions are predeclared:
`sizeOf(var)` returns the size of an array or a map.
`newMap()` creates a new empty map.
`{[key1]:[value1],[key2]:[value2]}` creates a new map.
`newList()` creates a new empty list.
`[1,2]` creates a new list.

View File

@ -339,7 +339,7 @@ public class ResolveGenerics {
} else if (val instanceof Integer)
sb.append("int(").append(val).append(")");
else if (val instanceof List) {
sb.append("newList(");
sb.append("[");
boolean first = true;
for (Object o : (List<?>) val) {
if (first)
@ -348,7 +348,7 @@ public class ResolveGenerics {
sb.append(",");
addToStringBuilder(sb, o);
}
sb.append(")");
sb.append("]");
} else if (val instanceof Map) {
sb.append("{");
boolean first = true;

View File

@ -56,7 +56,6 @@ public class Context implements HGSMap {
BUILT_IN.put("identifier", new FunctionIdentifier());
BUILT_IN.put("loadHex", new FunctionLoadHex());
BUILT_IN.put("sizeOf", new Func(1, args -> Value.toArray(args[0]).hgsArraySize()));
BUILT_IN.put("newList", new Func(-1, args -> new ArrayList<>(Arrays.asList(args))));
}
private final Context parent;

View File

@ -548,6 +548,8 @@ public class Parser {
return exp;
case OPENBRACE:
return parseStructLiteral();
case OPENSQUARE:
return parseListLiteral();
case FUNC:
FirstClassFunction func = parseFunction();
return c -> new FirstClassFunctionCall(func, c);
@ -556,6 +558,25 @@ public class Parser {
}
}
private Expression parseListLiteral() throws IOException, ParserException {
ArrayList<Expression> al = new ArrayList<>();
while (true) {
if (tok.peek() == CLOSEDSQUARE) {
tok.consume();
return c -> {
ArrayList<Object> l = new ArrayList<>();
for (Expression e : al)
l.add(e.value(c));
return l;
};
} else {
al.add(parseExpression());
if (tok.peek() == COMMA)
tok.consume();
}
}
}
private Expression parseStructLiteral() throws IOException, ParserException {
StructLiteral sl = new StructLiteral();
while (true) {

View File

@ -269,7 +269,7 @@ public class ParserTest extends TestCase {
assertEquals("Hello 9876543210 World!", c.toString());
c = exec("<? " +
"a:=newList(); " +
"a:=[];" +
"for (i:=0;i<10;i++) a[i]:=i; " +
"for (i:=0;i<10;i++) a[i]=9-i; " +
"for (i:=0;i<10;i++) print(a[i]); " +
@ -277,7 +277,7 @@ public class ParserTest extends TestCase {
assertEquals("9876543210", c.toString());
c = exec("<? " +
"a:=newList(); " +
"a:=[]; " +
"for (i:=0;i<5;i++) {" +
" for (j:=0;j<5;j++) a[i*5+j]:=i*j; " +
"}" +
@ -321,7 +321,7 @@ public class ParserTest extends TestCase {
}
public void testParseTemplateArray() throws IOException, ParserException, HGSEvalException {
Context c = exec("<? a:=newList(); a[0]:=1; a[1]:=7; print(a[1], \",\" ,sizeOf(a)); ?>;");
Context c = exec("<? a:=[]; a[0]:=1; a[1]:=7; print(a[1], \",\" ,sizeOf(a)); ?>;");
assertEquals("7,2;", c.toString());
Object lo = c.getVar("a");
assertTrue(lo instanceof List);
@ -332,7 +332,7 @@ public class ParserTest extends TestCase {
}
public void testParseTemplateArray2() throws IOException, ParserException, HGSEvalException {
Context c = exec("<? a:=newList(1,7); print(a[1], \",\" ,sizeOf(a)); ?>;");
Context c = exec("<? a:=[1,7]; print(a[1], \",\" ,sizeOf(a)); ?>;");
assertEquals("7,2;", c.toString());
Object lo = c.getVar("a");
assertTrue(lo instanceof List);
@ -453,7 +453,7 @@ public class ParserTest extends TestCase {
new Context().declareVar("a", 3)).toString());
assertEquals("18", exec("<? m:={f:func(a){return {v:a*a+2};}}; print(m.f(4).v);?>").toString());
assertEquals("18", exec("<? m:=newList(); m[0]:=func(a){ l:=newList(); l[0]:=a*a+2; return l;}; print(m[0](4)[0]);?>").toString());
assertEquals("18", exec("<? m:=[func(a){ return [a*a+2];}]; print(m[0](4)[0]);?>").toString());
failToEval("<? return 1; ?>", new Context());
failToEval("<? f:=func(a){return a;}; f(1)=5; ?>", new Context());

View File

@ -33,7 +33,7 @@
<elementAttributes>
<entry>
<string>generic</string>
<string>this.inverterConfig=newList(&quot;D&quot;);</string>
<string>this.inverterConfig=[&quot;D&quot;];</string>
</entry>
</elementAttributes>
<pos x="0" y="-100"/>