Skip to content

Commit 40715ca

Browse files
committed
Changed unary not to "!", added core math module, three quotes for block strings
1 parent 046d0eb commit 40715ca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1091
-643
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ All documentation can be found in the [wiki](https://github.com/tomdodd4598/Dodd
1616
Planned Features
1717
----------------
1818

19+
- Combine TypeElement into ClassElement
1920
- Various keywords replaced with static or member functions.
2021

2122
Permissions

run/classes.dssl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,25 +95,25 @@ item .printRecursive
9595

9696
/i 0.0 1.0 Complex new def
9797

98-
/binOpSetup "dup .re /xre exch def .im /xim exch def dup .re /yre exch def .im /yim exch def" def
98+
/binaryDefs "dup .re /xre exch def .im /xim exch def dup .re /yre exch def .im /yim exch def" def
9999

100100
/add {
101-
Complex .binOpSetup interpret
101+
Complex .binaryDefs interpret
102102
xre yre + xim yim + Complex new
103103
} magic
104104

105105
/sub {
106-
Complex .binOpSetup interpret
106+
Complex .binaryDefs interpret
107107
xre yre - xim yim - Complex new
108108
} magic
109109

110110
/mul {
111-
Complex .binOpSetup interpret
111+
Complex .binaryDefs interpret
112112
xre yre * xim yim * - xre yim * xim yre * + Complex new
113113
} magic
114114

115115
/div {
116-
Complex .binOpSetup interpret
116+
Complex .binaryDefs interpret
117117
/ysq yre yre * yim yim * + def
118118
xre yre * xim yim * + ysq / xim yre * xre yim * - ysq / Complex new
119119
} magic

src/dssl.sable

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@ Helpers
1616

1717
apostrophe = 39;
1818
quote = 34;
19-
tick = 96;
19+
multi_quote = quote quote quote;
2020
not_eol = [all - [cr + lf]];
2121
escape_char = '\' not_eol;
22-
multi_tick = tick tick tick;
2322

2423
c_char = [all - [apostrophe + ['\' + [lf + cr]]]] | escape_char;
2524
c_char_sequence = c_char+;
2625
s_char = [all - [quote + ['\' + [lf + cr]]]] | escape_char;
2726
s_char_sequence = s_char*;
28-
b_char = [all - [tick + '\']] | escape_char;
29-
b_char_sequence = b_char*;
3027

3128
not_star = [all - '*'];
3229
not_star_slash = [not_star - '/'];
@@ -176,17 +173,17 @@ Tokens
176173
idivide = '//';
177174
modulo = '%%';
178175

179-
not = 'not';
180-
neg = 'neg';
176+
not = '!';
181177

182178
int_value = sign? digit+;
183179
bool_value = 'true' | 'false';
184180
float_value = sign? (digit+ '.' digit* | '.' digit+);
185181
char_value = apostrophe c_char apostrophe;
186182

187183
line_string_value = 'r'? quote s_char_sequence quote;
188-
block_string_value = 'r'? multi_tick whitespace* eol b_char_sequence multi_tick;
184+
block_string_value = 'r'? multi_quote whitespace* eol s_char_sequence multi_quote;
189185

190186
identifier = name;
191187
label = '/' name;
192188
member = '.' name;
189+
module = '$' name;

src/dssl/Helpers.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ public static Token getLexerNext(Lexer lexer) {
6666
}
6767
}
6868

69+
public static <T> @NonNull T checkNonNull(T value) {
70+
if (value == null) {
71+
throw new IllegalArgumentException(String.format("Encountered invalid null value!"));
72+
}
73+
else {
74+
return value;
75+
}
76+
}
77+
6978
public static String lowerCase(String str) {
7079
return str.toLowerCase(Locale.ROOT);
7180
}
@@ -99,15 +108,14 @@ public static int leadingWhitespaceCount(String str) {
99108
unescapeMap.put("\\\\", "\\");
100109
unescapeMap.put("\\'", "'");
101110
unescapeMap.put("\\\"", "\"");
102-
unescapeMap.put("\\`", "`");
103111
unescapeMap.put("\\", "");
104112

105113
UNESCAPE_TRANSLATOR = new LookupTranslator(unescapeMap);
106114
}
107115

108-
public static @NonNull String parseString(String str) {
116+
public static @NonNull String parseString(String str, int prefixLength, int suffixLength) {
109117
boolean raw = str.charAt(0) == 'r';
110-
int start = raw ? 2 : 1, end = str.length() - 1;
118+
int start = (raw ? 1 : 0) + prefixLength, end = str.length() - suffixLength;
111119
String parsed = raw ? str.substring(start, end) : UNESCAPE_TRANSLATOR.translate(CharBuffer.wrap(str, start, end));
112120
if (parsed == null) {
113121
throw new RuntimeException(String.format("Failed to parse token %s!", str));
@@ -116,19 +124,19 @@ public static int leadingWhitespaceCount(String str) {
116124
}
117125

118126
public static @NonNull Character parseChar(String str) {
119-
String parsed = parseString(str);
127+
String parsed = parseString(str, 1, 1);
120128
if (parsed.length() != 1) {
121129
throw new IllegalArgumentException(String.format("Character value %s is invalid!", str));
122130
}
123131
return parsed.charAt(0);
124132
}
125133

126134
public static @NonNull String parseLineString(String str) {
127-
return parseString(str);
135+
return parseString(str, 1, 1);
128136
}
129137

130138
public static @NonNull String parseBlockString(String str) {
131-
String[] lines = parseString(str).split("\\r?\\n");
139+
String[] lines = parseString(str, 3, 3).split("\\r?\\n");
132140
boolean[] blanks = new boolean[lines.length];
133141
int commonWhitespace = -1;
134142
for (int i = 1; i < lines.length; ++i) {
@@ -189,7 +197,7 @@ public static <A, B> Set<B> map(Set<A> set, Function<A, B> function) {
189197
return Collectors.toMap(x -> keyFunction.apply(x.getKey()), x -> valueFunction.apply(x.getValue()), mergeFunction);
190198
}
191199

192-
public static final Set<String> KEYWORDS;
200+
public static final Set<String> KEYWORDS = new HashSet<>();
193201

194202
public static final String L_BRACE = "{";
195203
public static final String R_BRACE = "}";
@@ -333,8 +341,6 @@ public static <A, B> Set<B> map(Set<A> set, Function<A, B> function) {
333341
public static final String NEG = "neg";
334342

335343
static {
336-
KEYWORDS = new HashSet<>();
337-
338344
KEYWORDS.add(L_BRACE);
339345
KEYWORDS.add(R_BRACE);
340346

src/dssl/Hierarchy.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dssl;
22

33
import java.util.*;
4+
import java.util.Map.Entry;
45

56
import org.eclipse.jdt.annotation.Nullable;
67

@@ -39,6 +40,15 @@ protected Hierarchy(Map<K, V> internal, List<Hierarchy<K, V>> parents) {
3940
return prev;
4041
}
4142

43+
public void putAll(Hierarchy<K, V> other, boolean shadow) {
44+
for (Entry<K, V> entry : other.internal.entrySet()) {
45+
put(entry.getKey(), entry.getValue(), shadow);
46+
}
47+
for (Hierarchy<K, V> parent : other.parents) {
48+
putAll(parent, shadow);
49+
}
50+
}
51+
4252
public @Nullable V get(K key) {
4353
if (internal.containsKey(key)) {
4454
return internal.get(key);

src/dssl/Main.java

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,25 @@ public String read() {
7272

7373
Module moduleImpl = new Module() {
7474

75-
// TODO: INTRINSIC MODULES
76-
7775
@Override
7876
public TokenResult onInclude(TokenExecutor exec) {
7977
@NonNull Element elem = exec.pop();
8078
StringElement stringElem = elem.stringCastImplicit();
81-
if (stringElem == null) {
82-
throw new IllegalArgumentException(String.format("Keyword \"include\" requires string value element as argument!"));
79+
if (stringElem != null) {
80+
try (PushbackReader reader = Helpers.getPushbackReader(new FileReader(stringElem.toString()))) {
81+
return new TokenExecutor(new LexerIterator(reader), exec, false).iterate();
82+
}
83+
catch (Exception e) {
84+
e.printStackTrace();
85+
throw new RuntimeException();
86+
}
8387
}
84-
85-
try (PushbackReader reader = Helpers.getPushbackReader(new FileReader(stringElem.toString()))) {
86-
return new TokenExecutor(new LexerIterator(reader), exec, false).iterate();
88+
else if (elem instanceof ModuleElement) {
89+
exec.putAll(((ModuleElement) elem).clazz, true);
90+
return TokenResult.PASS;
8791
}
88-
catch (Exception e) {
89-
e.printStackTrace();
90-
throw new RuntimeException();
92+
else {
93+
throw new IllegalArgumentException(String.format("Keyword \"include\" requires string or module element as argument!"));
9194
}
9295
}
9396

@@ -99,19 +102,23 @@ public TokenResult onImport(TokenExecutor exec) {
99102
}
100103

101104
StringElement stringElem = elem1.stringCastImplicit();
102-
if (stringElem == null) {
103-
throw new IllegalArgumentException(String.format("Keyword \"import\" requires string value element as second argument!"));
105+
if (stringElem != null) {
106+
try (PushbackReader reader = Helpers.getPushbackReader(new FileReader(stringElem.toString()))) {
107+
TokenExecutor otherExec = exec.interpreter.newExecutor(new LexerIterator(reader));
108+
((LabelElement) elem0).setClazz(otherExec, new ArrayList<>());
109+
return otherExec.iterate();
110+
}
111+
catch (Exception e) {
112+
e.printStackTrace();
113+
throw new RuntimeException();
114+
}
104115
}
105-
106-
try (PushbackReader reader = Helpers.getPushbackReader(new FileReader(stringElem.toString()))) {
107-
TokenExecutor otherExec = exec.interpreter.newExecutor(new LexerIterator(reader));
108-
TokenResult result = otherExec.iterate();
109-
((LabelElement) elem0).setClazz(otherExec, new ArrayList<>());
110-
return result;
116+
else if (elem1 instanceof ModuleElement) {
117+
((LabelElement) elem0).setClazz(((ModuleElement) elem1).clazz, new ArrayList<>());
118+
return TokenResult.PASS;
111119
}
112-
catch (Exception e) {
113-
e.printStackTrace();
114-
throw new RuntimeException();
120+
else {
121+
throw new IllegalArgumentException(String.format("Keyword \"import\" requires string or module element as second argument!"));
115122
}
116123
}
117124
};

src/dssl/NativeImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public TokenResult onNative(TokenExecutor exec) {
2121
@NonNull Element elem = exec.pop();
2222
StringElement stringElem = elem.stringCastImplicit();
2323
if (stringElem == null) {
24-
throw new IllegalArgumentException(String.format("Keyword \"native\" requires string value element as last argument!"));
24+
throw new IllegalArgumentException(String.format("Keyword \"native\" requires string element as last argument!"));
2525
}
2626

2727
String str = stringElem.toString();

src/dssl/analysis/Analysis.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ public interface Analysis extends Switch
120120
void caseTIdivide(TIdivide node);
121121
void caseTModulo(TModulo node);
122122
void caseTNot(TNot node);
123-
void caseTNeg(TNeg node);
124123
void caseTIntValue(TIntValue node);
125124
void caseTBoolValue(TBoolValue node);
126125
void caseTFloatValue(TFloatValue node);
@@ -130,6 +129,7 @@ public interface Analysis extends Switch
130129
void caseTIdentifier(TIdentifier node);
131130
void caseTLabel(TLabel node);
132131
void caseTMember(TMember node);
132+
void caseTModule(TModule node);
133133
void caseEOF(EOF node);
134134
void caseInvalidToken(InvalidToken node);
135135
}

src/dssl/analysis/AnalysisAdapter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -722,12 +722,6 @@ public void caseTNot(TNot node)
722722
defaultCase(node);
723723
}
724724

725-
@Override
726-
public void caseTNeg(TNeg node)
727-
{
728-
defaultCase(node);
729-
}
730-
731725
@Override
732726
public void caseTIntValue(TIntValue node)
733727
{
@@ -782,6 +776,12 @@ public void caseTMember(TMember node)
782776
defaultCase(node);
783777
}
784778

779+
@Override
780+
public void caseTModule(TModule node)
781+
{
782+
defaultCase(node);
783+
}
784+
785785
@Override
786786
public void caseEOF(EOF node)
787787
{

src/dssl/interpret/BuiltIn.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package dssl.interpret;
2+
3+
import java.util.*;
4+
5+
import org.eclipse.jdt.annotation.NonNull;
6+
7+
public class BuiltIn {
8+
9+
public static final Map<@NonNull String, Clazz> CLAZZ_MAP = new HashMap<>();
10+
public static final Map<@NonNull String, Clazz> MODULE_MAP = new HashMap<>();
11+
12+
public static final @NonNull Clazz INT_CLAZZ;
13+
public static final @NonNull Clazz BOOL_CLAZZ;
14+
public static final @NonNull Clazz FLOAT_CLAZZ;
15+
public static final @NonNull Clazz CHAR_CLAZZ;
16+
public static final @NonNull Clazz STRING_CLAZZ;
17+
18+
public static final @NonNull Clazz RANGE_CLAZZ;
19+
public static final @NonNull Clazz LIST_CLAZZ;
20+
public static final @NonNull Clazz TUPLE_CLAZZ;
21+
public static final @NonNull Clazz SET_CLAZZ;
22+
public static final @NonNull Clazz DICT_CLAZZ;
23+
24+
public static final @NonNull Clazz MATH_MODULE;
25+
26+
static {
27+
INT_CLAZZ = clazz("int");
28+
BOOL_CLAZZ = clazz("bool");
29+
FLOAT_CLAZZ = clazz("float");
30+
CHAR_CLAZZ = clazz("char");
31+
STRING_CLAZZ = clazz("string");
32+
33+
RANGE_CLAZZ = clazz("range");
34+
LIST_CLAZZ = clazz("list");
35+
TUPLE_CLAZZ = clazz("tuple");
36+
SET_CLAZZ = clazz("set");
37+
DICT_CLAZZ = clazz("dict");
38+
39+
MATH_MODULE = module("math");
40+
}
41+
42+
static @NonNull Clazz clazz(@NonNull String name) {
43+
Clazz clazz = new Clazz(name);
44+
CLAZZ_MAP.put(name, clazz);
45+
return clazz;
46+
}
47+
48+
static @NonNull Clazz module(@NonNull String name) {
49+
Clazz module = new Clazz(name);
50+
MODULE_MAP.put(name, module);
51+
return module;
52+
}
53+
54+
static {
55+
MATH_MODULE.setMacro("neg", TokenExecutor::neg);
56+
MATH_MODULE.setMacro("abs", TokenExecutor::abs);
57+
MATH_MODULE.setMacro("sgn", TokenExecutor::sgn);
58+
MATH_MODULE.setMacro("floor", TokenExecutor::floor);
59+
MATH_MODULE.setMacro("ceil", TokenExecutor::ceil);
60+
MATH_MODULE.setMacro("round", TokenExecutor::round);
61+
62+
MATH_MODULE.setMacro("sin", TokenExecutor::sin);
63+
MATH_MODULE.setMacro("cos", TokenExecutor::cos);
64+
MATH_MODULE.setMacro("tan", TokenExecutor::tan);
65+
MATH_MODULE.setMacro("asin", TokenExecutor::asin);
66+
MATH_MODULE.setMacro("acos", TokenExecutor::acos);
67+
MATH_MODULE.setMacro("atan", TokenExecutor::atan);
68+
69+
MATH_MODULE.setMacro("sinc", TokenExecutor::sinc);
70+
MATH_MODULE.setMacro("atan2", TokenExecutor::atan2);
71+
72+
MATH_MODULE.setMacro("rads", TokenExecutor::rads);
73+
MATH_MODULE.setMacro("degs", TokenExecutor::degs);
74+
75+
MATH_MODULE.setMacro("exp", TokenExecutor::exp);
76+
MATH_MODULE.setMacro("log", TokenExecutor::log);
77+
MATH_MODULE.setMacro("log10", TokenExecutor::log10);
78+
MATH_MODULE.setMacro("sqrt", TokenExecutor::sqrt);
79+
MATH_MODULE.setMacro("cbrt", TokenExecutor::cbrt);
80+
81+
MATH_MODULE.setMacro("expm1", TokenExecutor::expm1);
82+
MATH_MODULE.setMacro("log1p", TokenExecutor::log1p);
83+
84+
MATH_MODULE.setMacro("sinh", TokenExecutor::sinh);
85+
MATH_MODULE.setMacro("cosh", TokenExecutor::cosh);
86+
MATH_MODULE.setMacro("tanh", TokenExecutor::tanh);
87+
MATH_MODULE.setMacro("asinh", TokenExecutor::asinh);
88+
MATH_MODULE.setMacro("acosh", TokenExecutor::acosh);
89+
MATH_MODULE.setMacro("atanh", TokenExecutor::atanh);
90+
91+
MATH_MODULE.setMacro("min", TokenExecutor::min);
92+
MATH_MODULE.setMacro("max", TokenExecutor::max);
93+
}
94+
}

0 commit comments

Comments
 (0)