Permalink
Browse files

Some more docs on operators and fixes regarding field order in objects.

  • Loading branch information...
neilellis committed Sep 28, 2017
1 parent ec99ce6 commit ed7b04b972fc7f8b905625e4c7230fff2eece27d
Showing with 104 additions and 23 deletions.
  1. +5 −1 dollar-core/src/main/java/dollar/api/DollarException.java
  2. +4 −0 dollar-core/src/main/java/dollar/api/VarKey.java
  3. +5 −1 dollar-core/src/main/java/dollar/api/types/DollarMap.java
  4. +5 −5 dollar-docs/src/main/resources/pages/manual.md
  5. +3 −3 dollar-script/src/main/java/dollar/internal/runtime/script/obj/DollarObject.java
  6. +12 −8 dollar-script/src/main/java/dollar/internal/runtime/script/parser/Symbols.java
  7. +5 −4 dollar-script/src/main/java/dollar/internal/runtime/script/parser/scope/ScriptScope.java
  8. +3 −0 dollar-script/src/main/resources/examples/op/and.ds
  9. +2 −0 dollar-script/src/main/resources/examples/op/assert.ds
  10. +2 −0 dollar-script/src/main/resources/examples/op/avg.ds
  11. +1 −0 dollar-script/src/main/resources/examples/op/avg.md
  12. +4 −0 dollar-script/src/main/resources/examples/op/block.ds
  13. +18 −0 dollar-script/src/main/resources/examples/op/block.md
  14. +1 −0 dollar-script/src/main/resources/examples/op/default.ds
  15. +1 −1 dollar-script/src/main/resources/examples/op/destroy.md
  16. +2 −0 dollar-script/src/main/resources/examples/op/sort.ds
  17. 0 dollar-script/src/main/resources/examples/op/{sorted.ds → sort.md}
  18. 0 dollar-script/src/main/resources/examples/op/sorted.md
  19. +11 −0 dollar-script/src/main/resources/examples/op/split.ds
  20. +1 −0 dollar-script/src/main/resources/examples/op/split.md
  21. +2 −0 dollar-script/src/main/resources/examples/op/sum.ds
  22. +1 −0 dollar-script/src/main/resources/examples/op/sum.md
  23. +9 −0 dollar-script/src/main/resources/examples/op/truthy.ds
  24. +2 −0 dollar-script/src/main/resources/examples/op/unique.ds
  25. +5 −0 dollar-script/src/main/resources/examples/op/while.ds
@@ -64,7 +64,11 @@ public DollarException(@NotNull Exception cause, @NotNull String message) {
public static @NotNull
Exception unravel(@NotNull Exception e) {
if ((e instanceof InvocationTargetException) || (e instanceof ExecutionException)) {
return unravel((Exception) e.getCause());
Throwable cause = e.getCause();
if ((cause instanceof Error)) {
throw (Error) cause;
}
return unravel((Exception) cause);
} else {
return e;
}
@@ -101,6 +101,10 @@ public String toString() {
return key;
}
public boolean isAlphaNumeric() {
return key.matches("[a-zA-Z0-9]+");
}
public boolean isNumeric() {
return key.matches("[0-9]+");
}
@@ -509,7 +509,11 @@ public boolean pair() {
@Override
public int compareTo(@NotNull Value o) {
return Comparator.<Value>naturalOrder().<Value>compare(this, o);
if (pair() && o.pair()) {
return Comparator.<Value>naturalOrder().<Value>compare($pairKey(), o.$pairKey());
} else {
throw new DollarFailureException(ErrorType.INVALID_MAP_OPERATION, "Cannot compare maps");
}
}
@NotNull
@@ -204,7 +204,7 @@ So as you can see when we use the `=` assignment operator we assign the *value*
The assignment operator `=` has an infinite 'fix' depth This means that any expression will be evaluated completely also it means the result is not reactive.
The assert equivalence operator `<=>` will compare two values and throw an exception if they are not the same at any point **proceeding** the expression, ` a <=> b` is the same as `.: a == b`**
The `always` operator `<=>` will compare two values and throw an exception if they are not the same at any point **at or after** the expression, ` a <=> b` is the same as `.: a == b`.
The assert equals operator `<->` will compare two values only at the point that the expression occurs. It is roughly the same as .equals() in Java and is the equivalent of `.: &a == &b`
@@ -233,14 +233,14 @@ Note that `def` implies `const`, `def` means define and therefore not variable.
#### Summary
> It's important to note that all values in Dollar are immutable - that means if you wish to change the value of a variable you *must* __reassign__ a new value to the variable. For example `v++` would return the value of `v+1` it does not increment v. If however you want to assign a constant value, one that is both immutable and cannot be reassigned, just use the `const` modifier at the variable assignment (this does not make sense for declarations, so is only available on assignments).
> It's important to note that all values in Dollar are immutable - that means if you wish to change the value of a variable you *must* __reassign__ a new value to the variable. For example `v++` would return the value of `v+1` it does not increment v. If however you want to assign a constant value, one that is both immutable and cannot be reassigned, just use the `const` modifier at the variable declaration.
```dollar
const MEDIUM = 23
// MEDIUM= 4 would now produce an error
```
So `:=` supports the full reactive behaviour of Dollar, i.e. it is a declaration not a value assignment, and `=` is used to nail down a particular value or reduce the reactive behaviour. Later we'll come across the fix operator `&` which instructs Dollar to fix a value completely . More on that later.
So `:=` supports the full reactive behaviour of Dollar, i.e. it is a definition not a value assignment, and `=` is used to nail down a particular value or reduce the reactive behaviour. Later we'll come across the fix operator `&` which instructs Dollar to fix a value completely.
### Blocks
@@ -285,7 +285,7 @@ list2 <=> [1,2]
#### Map Block
Finally we have the map block, when an map block is evaluated the result is the aggregation of the parts from top to bottom into a map. The map block starts and finishes with the `{` `}` braces, however each part is separated by a `,` not a `;` or *newline* . The default behaviour of a map block is virtually useless, it takes the string value and makes it the key and keeps the original value as the value to be paired with that key.
Finally we have the map block, when an map block is evaluated the result is the aggregation of the parts from top to bottom into a map. The map block starts and finishes with the `{` `}` braces, however each part is separated by a `,` or *newline* not a `;`. The default behaviour of a map block is virtually useless, it takes each value and makes it's String value the key and the original value is the value paired with that key.
```dollar
@@ -320,7 +320,7 @@ mapBlock.second <=> "World"
A map block with one entry that is not a pair is assumed to be a *Line Block*.
The stdout operator `@@` is used to send a value to stdout in it's serialized (JSON) format, so the result of the above would be to output `{"first":"Hello ","second":"World"}` a JSON object created using JSON like syntax. Maps can also be created by joining pairs.
> The stdout operator `@@` is used to send a value to stdout in it's serialized (JSON) format, so the result of the above would be to output `{"first":"Hello ","second":"World"}` a JSON object created using JSON like syntax. Maps can also be created by joining pairs.
```dollar
@@ -42,7 +42,6 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import static dollar.api.types.DollarFactory.FALSE;
@@ -54,7 +53,7 @@
@NotNull
private final Value constructor;
@NotNull
private final Map<VarKey, Variable> fields = new ConcurrentHashMap<>();
private final Map<VarKey, Variable> fields = new LinkedHashMap<>();
@NotNull
private final String name;
@NotNull
@@ -402,7 +401,8 @@ public Number toNumber() {
final List<Value> entries =
fields.entrySet()
.stream()
.map(entry -> DollarStatic.$(entry.getKey(), entry.getValue()))
.filter(i -> i.getKey().isAlphaNumeric())
.map(entry -> DollarStatic.$(entry.getKey().asString(), entry.getValue().getValue()))
.collect(Collectors.toList());
return ImmutableList.copyOf(entries);
}
@@ -65,17 +65,21 @@
@NotNull
public static final KeywordDef AS = new KeywordDef("as", false, null, null);
@NotNull
public static final Op AVG = new Op(OpType.POSTFIX, "[%]", "avg", "avg",
false, true,
null, PLUS_MINUS_PRIORITY, true, NO_SCOPE, null, ANY_TYPE_F);
public static final Op AVG = new Op(OpType.POSTFIX, "[%]", "avg", "avg", false, true,
" ( 'avg' <list-expression> ) | ( <list-expression> '[%]' )", PLUS_MINUS_PRIORITY, true,
NO_SCOPE, null, ANY_TYPE_F);
@NotNull
public static final KeywordDef BLOCK = new KeywordDef("block", false, null, null);
@NotNull
public static final Op BLOCK_OP = new Op(OpType.COLLECTION, null, null, "block",
false, true,
"'{' ( <expression> ';' ) * [ <expression> ] '}'",
NO_PRIORITY, null, SCOPE_WITH_CLOSURE, null, i -> Type._BLOCK);
@NotNull
public static final Op AND = new Op(OpType.BINARY, "&&", "and", "and",
false, true,
@@ -289,7 +293,7 @@
@NotNull
public static final Op PARALLEL = new Op(OpType.PREFIX, "|:|", "parallel", "parallel",
false, false,
null, SIGNAL_PRIORITY, true, NEW_PARALLEL_SCOPE, ":vertical_traffic_light:",
null, SIGNAL_PRIORITY, true, NO_SCOPE, ":vertical_traffic_light:",
FIRST_TYPE_F);
@NotNull
public static final Op PARAM_OP = new Op(OpType.POSTFIX, null, null, "parameter", false, true,
@@ -372,14 +376,14 @@
public static final SymbolDef SEMI_COLON = new SymbolDef(";", false);
@NotNull
public static final Op SERIAL = new Op(OpType.PREFIX, "|..|", "serial", "serial",
false, false,
null, SIGNAL_PRIORITY, true, NEW_SERIAL_SCOPE, ":traffic_light:", FIRST_TYPE_F);
true, false,
null, SIGNAL_PRIORITY, true, NO_SCOPE, ":traffic_light:", FIRST_TYPE_F);
@NotNull
public static final Op SIZE = new Op(OpType.PREFIX, "#", null, "size",
false, true,
null, UNARY_PRIORITY, true, NO_SCOPE, ":hash:", INTEGER_TYPE_F);
@NotNull
public static final Op SORT = new Op(OpType.PREFIX, "->", "sorted", "sorted",
public static final Op SORT = new Op(OpType.PREFIX, "->", "sort", "sort",
false, true,
null, SORT_PRIORITY, true, NO_SCOPE, null, FIRST_TYPE_F);
@NotNull
@@ -801,7 +805,6 @@
RIGHT_BRACKET,
RIGHT_PAREN,
SEMI_COLON,
SERIAL,
START,
STATE,
STOP,
@@ -853,6 +856,7 @@
WRITE_OP,
//Reserved Operators
SERIAL,
RESERVED_OPERATOR_2,
RESERVED_OPERATOR_3,
RESERVED_OPERATOR_4,
@@ -18,7 +18,6 @@
import com.google.common.base.Objects;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;
import dollar.api.ClassName;
import dollar.api.DollarClass;
@@ -50,7 +49,9 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@@ -83,7 +84,7 @@
@NotNull
private final UUID uuid = UUID.randomUUID();
@NotNull
private final ConcurrentHashMap<VarKey, Variable> variables = new ConcurrentHashMap<>();
private final Map<VarKey, Variable> variables = Collections.<VarKey, Variable>synchronizedMap(new LinkedHashMap());
@Nullable
Scope parent;
@Nullable
@@ -142,7 +143,7 @@ public ScriptScope(@NotNull Scope parent,
private ScriptScope(@NotNull Scope parent,
@NotNull String id,
boolean parameterScope,
@NotNull ConcurrentHashMap<VarKey, Variable> variables,
@NotNull Map<VarKey, Variable> variables,
@NotNull List<Value> errorHandlers,
@NotNull Multimap<VarKey, Listener> listeners,
@NotNull String source,
@@ -728,7 +729,7 @@ public Variable variable(@NotNull VarKey key) {
@NotNull
@Override
public Map<VarKey, Variable> variables() {
return ImmutableMap.copyOf(variables);
return new LinkedHashMap<>(variables);
}
// --Commented out by Inspection START (10/09/2017, 14:29):
@@ -2,3 +2,6 @@ true && true <=> true
true && false <=> false
false && true <=> false
false && false <=> false
true and true <=> true
false and true always false
@@ -2,3 +2,5 @@
.: 3 > 2
.: 1 <= 1
.: 1 <= 2
assert 1 < 2
@@ -0,0 +1,2 @@
[1,2,3,4,5][%] <=> 3
avg [1,2,3,4,5] <=> 3
@@ -0,0 +1 @@
The average operation `avg` or `[%]` calculates the average value of a list.
@@ -0,0 +1,4 @@
const aBlock := {
@@ "The first command"
@@ "The second command"
}
@@ -0,0 +1,18 @@
A line block lies between `{` and `}` and is separated by either newlines or `;` characters.
```dollar
var myBlock = {
"Hello "
"World"
}
myBlock <=> "World"
const myBlock2 = {1;2}
myBlock2 <=> 2
```
When a line block is evaluated the result is the value of the last entry. For advanced users note that all lines will be evaluated, the value is just ignored. A line block behaves a lot like a function in an imperative language.
@@ -1,2 +1,3 @@
void :- "Hello" <=> "Hello"
1 :- "Hello" <=> 1
1 default "Hello" <=> 1
@@ -1 +1 @@
destroy
TODO:
@@ -0,0 +1,2 @@
->[3,1,2] <=> [1,2,3]
sort [3,1,2] <=> [1,2,3]
@@ -0,0 +1,11 @@
sort ({"first":1,"second":2,"third":3}[/]) <=> [{"first":1},{"second":2},{"third":3}]
class TestClass {
var first=1;
var second=2;
var third=3;
}
new TestClass() [/] <=> [{"first":1},{"second":2},{"third":3}]
// Single value types become single value lists.
1[/] <=> [1]
@@ -0,0 +1 @@
The list split operator `[/]` will convert any non list type to a list with division as the preferred method. The split operator will split a Map or an Object into pairs for example.
@@ -0,0 +1,2 @@
[1,2,3,4,5][+] <=> 15
["a","b","c"][+] <=> "abc"
@@ -0,0 +1 @@
The sum operator reduces a list using the `+` operation.
@@ -0,0 +1,9 @@
.: ~ [1,2,3]
.: ! ~ []
.: ~ "anything"
.: ! ~ ""
.: ~ 1
.: ! ~ 0
.: ! ~ {void}
.: ~ { "a" : 1}
.: ! ~ void
@@ -0,0 +1,2 @@
[1,2,2,3][!] <=> [1,2,3]
["aaa","a","aaa","b"][!] <=> ["aaa","a","b"]
@@ -0,0 +1,5 @@
var a= 1;
while a < 10 {
print a
a=a+1;
}

0 comments on commit ed7b04b

Please sign in to comment.