Skip to content

Commit 9951584

Browse files
committed
0.0.2 - Abstracts in scripts
0.0.2 : Added : - Abstract statics support - Date and Datetools defaults for RuleScriptInterp Fixed : - Fixed bug when code ignored properties
1 parent 37e7522 commit 9951584

File tree

14 files changed

+277
-29
lines changed

14 files changed

+277
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/.vscode
33
/test/export
44
hxformat.json
5+
/test/dump

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Changelog
2+
3+
# 0.0.2 :
4+
5+
### Added :
6+
- Abstract statics support
7+
- Date and Datetools defaults for RuleScriptInterp
8+
9+
### Fixed :
10+
- Fixed bug when code ignored properties
11+
12+
# 0.0.1 :
13+
14+
### Initial Release

README.md

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ Hscript addon with imports, usings, string interpolation and more.
44

55
## Features:
66

7-
### Package :
7+
### Package
88
package keyword (optional).
99
```haxe
1010
package scripts.hello.world;
1111
```
12-
### Import :
12+
### Import
1313
```haxe
1414
import haxe.ds.StringMap;
1515
1616
var map = new StringMap();
1717
map.set("Hello","World");
1818
trace(map.get("Hello")); // World
1919
```
20-
### Import with alias:
20+
### Import with alias
2121
you can use `as` or `in` keywords alias.
2222
```haxe
2323
import haxe.ds.StringMap as StrMap;
@@ -34,7 +34,7 @@ map.set("Hello","World");
3434
trace(map.get("Hello")); // World
3535
```
3636

37-
### Using:
37+
### Using
3838
```haxe
3939
using Reflect;
4040
@@ -43,12 +43,12 @@ var a = {
4343
};
4444
trace(a.getProperty("Hello")); // World
4545
```
46-
46+
4747
### String interpolation (Experimental)
4848
RuleScript supports [String Interpolation](https://haxe.org/manual/lf-string-interpolation.html), but you can only use identifiers, double quotes string, calls without arguments and `+` operator.
4949
```haxe
5050
var a = 'Hello';
51-
return 'RuleScript: $a World'; // RuleScript: Hello World
51+
trace('RuleScript: $a World'); // RuleScript: Hello World
5252
```
5353
```haxe
5454
var a = {
@@ -57,33 +57,60 @@ var a = {
5757
c:'World'
5858
};
5959
60-
return '${a.a}: ${a.b() + " " + a.c}'; // RuleScript: Hello World
60+
trace('${a.a}: ${a.b() + " " + a.c}'); // RuleScript: Hello World
6161
```
6262

63+
### Abstracts in script
64+
65+
RuleScriptAbstracts.txt in any classpath :
66+
```
67+
test.HelloWorldAbstract
68+
```
69+
70+
test/HelloWorldAbstract.hx :
71+
```haxe
72+
@:keep
73+
abstract HelloWorldAbstract(String) from String to String
74+
{
75+
public static function rulescriptPrint():HelloWorldAbstract
76+
return 'Hello World';
77+
}
78+
```
79+
Script :
80+
```haxe
81+
import test.HelloWorldAbstract;
82+
83+
trace(HelloWorldAbstract.rulescriptPrint()); // 'Hello World'
84+
```
6385
More templates in `test/src/Main.hx`.
86+
6487
# Limitations
6588

6689
- [String interpolations](https://haxe.org/manual/lf-string-interpolation.html) don't support many functions
6790
- Script `using` callback supports max number of arguments is 8.
6891
- [Wildcard imports](https://haxe.org/manual/type-system-import.html#wildcard-import) don't support.
92+
- AbstractMacro ports only static fields
6993

70-
# To Do:
94+
# To Do
7195
- Lua Parser
72-
- Importing abstract classes in scripts
7396
- Improve String Interpolation
7497
- Improve hscript module parser
7598

7699
# Install
77100

78-
1. Installing lib :
79-
- Haxelib : `haxelib git rulescript https://github.com/Kriptel/RuleScript.git`
80-
- Hmm : `hmm git rulescript https://github.com/Kriptel/RuleScript.git`
81-
2. Adding lib to your project :
82-
- Hxml :
83-
```hxml
84-
-lib rulescript
85-
```
86-
- Lime/OpenFL :
87-
```xml
88-
<haxelib name="rulescript"/>
89-
```
101+
1. Installing lib:
102+
103+
Haxelib : `haxelib git rulescript https://github.com/Kriptel/RuleScript.git`
104+
105+
Hmm : `hmm git rulescript https://github.com/Kriptel/RuleScript.git`
106+
2. Adding lib to your project:
107+
108+
Hxml :
109+
```hxml
110+
-lib rulescript
111+
```
112+
113+
Lime/OpenFL :
114+
```xml
115+
<haxelib name="rulescript"/>
116+
```

extraParams.hxml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414
--macro addMetadata('@:build(rulescript.macro.ExprMacro.buildCheckerDefaults())', 'hscript.Checker')
1515

1616
--macro addMetadata('@:build(rulescript.macro.ExprMacro.buildMacroDefaults())', 'hscript.Macro')
17+
18+
--macro rulescript.macro.Converter.init()

haxelib.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"url" : "https://github.com/Kriptel/RuleScript",
44
"license": "MIT",
55
"description": "Hscript addon with imports, string interpolation and more",
6-
"version": "0.0.1",
7-
"releasenote": "Initial Release",
6+
"version": "0.0.2",
7+
"releasenote": "Abstracts in script",
88
"dependencies": {
99
"hscript":""
1010
},

rulescript/Abstracts.hx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package rulescript;
2+
3+
import rulescript.macro.AbstractMacro;
4+
5+
using StringTools;
6+
7+
class Abstracts
8+
{
9+
public static dynamic function resolveAbstract(name:String):Class<Dynamic>
10+
{
11+
return Type.resolveClass('rulescript.__abstracts.${name.substring(0, name.lastIndexOf('.') + 1) + '_' + name.substring(name.lastIndexOf('.') + 1)}');
12+
}
13+
}

rulescript/RuleScriptInterp.hx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class RuleScriptInterp extends hscript.Interp
2323
variables.set('Type', Type);
2424
variables.set('Reflect', Reflect);
2525
variables.set('StringTools', StringTools);
26+
variables.set('Date', Date);
27+
variables.set('DateTools', DateTools);
2628

2729
#if sys
2830
variables.set('Sys', Sys);
@@ -55,6 +57,9 @@ class RuleScriptInterp extends hscript.Interp
5557
if (t == null)
5658
t = Type.resolveClass(path);
5759

60+
if (t == null)
61+
t = Abstracts.resolveAbstract(path);
62+
5863
if (t == null)
5964
t = Type.resolveEnum(path);
6065

@@ -81,19 +86,23 @@ class RuleScriptInterp extends hscript.Interp
8186
return null;
8287
}
8388

89+
/**
90+
* hasField not works for properties
91+
* If getProperty object is null, interp tries to get prop from usings
92+
*/
8493
override function get(o:Dynamic, f:String):Dynamic
8594
{
86-
if (Reflect.hasField(o, f))
87-
return super.get(o, f);
95+
var prop:Dynamic = super.get(o, f);
96+
if (prop != null)
97+
return prop;
8898

8999
for (cl in usings)
90100
{
91-
if (Reflect.hasField(cl, f))
92-
return Tools.usingFunction.bind(o, Reflect.getProperty(cl, f), _, _, _, _, _, _, _, _);
101+
var prop:Dynamic = Reflect.getProperty(cl, f);
102+
if (prop != null)
103+
return Tools.usingFunction.bind(o, prop, _, _, _, _, _, _, _, _);
93104
}
94105

95-
error(ECustom('$o has no field $f'));
96-
97106
return null;
98107
}
99108
}

rulescript/macro/AbstractMacro.hx

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package rulescript.macro;
2+
3+
import haxe.macro.Context;
4+
import haxe.macro.Expr;
5+
import sys.FileSystem;
6+
import sys.io.File;
7+
8+
using StringTools;
9+
10+
class AbstractMacro
11+
{
12+
/**
13+
* Converts abstract to class
14+
*/
15+
#if !macro macro #end public static function buildAbstract(name:String, pack:String):Array<Field>
16+
{
17+
var fields = Context.getBuildFields();
18+
var pos = Context.currentPos();
19+
20+
var type = Context.getLocalClass();
21+
22+
var isEnum:Bool = type.get().meta.has(':enum');
23+
24+
var cl = macro class {};
25+
26+
var imports = Context.getLocalImports();
27+
28+
imports.push({
29+
path: name.split('.').map(s -> {
30+
name: s,
31+
pos: pos
32+
}),
33+
mode: INormal
34+
});
35+
36+
if (name.contains('.'))
37+
{
38+
var list = name.split('.');
39+
40+
while (list.length > 1)
41+
pack += '.' + list.shift();
42+
43+
name = list[0];
44+
}
45+
46+
cl.name = '_' + name;
47+
cl.pack = pack.split('.');
48+
49+
for (f in fields)
50+
{
51+
if (isEnum && (f.kind.match(FVar(_, _))))
52+
{
53+
cl.fields.push({
54+
name: f.name,
55+
doc: f.doc,
56+
access: [APublic, AStatic],
57+
kind: switch (f.kind)
58+
{
59+
case FVar(t, e) if (e != null):
60+
f.kind;
61+
default:
62+
FVar(macro :String, macro $v{f.name});
63+
},
64+
pos: f.pos,
65+
meta: f.meta
66+
});
67+
}
68+
else if (f.access.contains(AStatic))
69+
cl.fields.push(f);
70+
}
71+
72+
Context.defineModule('$pack._$name', [cl], imports);
73+
74+
return fields;
75+
}
76+
}

rulescript/macro/Converter.hx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package rulescript.macro;
2+
3+
import haxe.macro.Compiler;
4+
import haxe.macro.Context;
5+
import haxe.macro.Expr;
6+
import sys.FileSystem;
7+
import sys.io.File;
8+
9+
using StringTools;
10+
11+
class Converter
12+
{
13+
// Language server shows errors...
14+
public static function init()
15+
{
16+
var files:Array<String> = [];
17+
18+
var filename:String = Context.definedValue('rulescript_abstracts_file_path') ?? 'RuleScriptAbstracts.txt';
19+
20+
var abstractsList:Array<String> = [];
21+
22+
for (dir in Context.getClassPath())
23+
if (FileSystem.exists(dir + filename))
24+
for (abs in parseFile(File.getContent(dir + filename)))
25+
if (!abstractsList.contains(abs))
26+
abstractsList.push(abs);
27+
28+
for (name in abstractsList)
29+
Compiler.addMetadata('@:build(rulescript.macro.AbstractMacro.buildAbstract("$name","rulescript.__abstracts"))', name);
30+
}
31+
32+
static function parseFile(content:String):Array<String>
33+
{
34+
var text:String = content.replace('\r', '').replace(' ', '');
35+
return text.split('\n');
36+
}
37+
}

test/src/Main.hx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Main
2222
packageTest();
2323
importAndUsingTest();
2424
stringInterpolationTest();
25+
testAbstract();
2526
}
2627
catch (e)
2728
trace(e?.details());
@@ -135,6 +136,27 @@ class Main
135136
");
136137
}
137138

139+
public static function testAbstract()
140+
{
141+
runScript('
142+
import test.HelloWorldAbstract;
143+
144+
return HelloWorldAbstract.rulescriptPrint();
145+
');
146+
147+
runScript('
148+
import test.HelloWorldAbstract as Hw;
149+
150+
return Hw.rulescriptPrint();
151+
');
152+
153+
runScript("
154+
import test.HelloWorldAbstract as Hw;
155+
156+
return '${Hw.RULESCRIPT}: ${Hw.hello} ${Hw.world}';
157+
");
158+
}
159+
138160
public static function runScript(code:String)
139161
{
140162
Sys.println('\n[Running code #${++callNum}]: "$code"\n\n [Result]: ${script.tryExecute(code)}');

0 commit comments

Comments
 (0)