kiss insertion

This commit is contained in:
2022-06-08 01:36:36 +00:00
parent dd998372e2
commit b6786586f7
3 changed files with 49 additions and 4 deletions

View File

@@ -4,6 +4,7 @@ package kiss;
import haxe.Exception;
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.ExprTools;
import haxe.io.Path;
import kiss.Stream;
import kiss.Reader;
@@ -56,8 +57,10 @@ typedef KissState = {
collectedBlocks:Map<String, Array<ReaderExp>>,
inStaticFunction:Bool
};
#end
class Kiss {
#if macro
public static function defaultKissState():KissState {
var className = Context.getLocalClass().get().name;
@@ -202,7 +205,34 @@ class Kiss {
return macro {};
}
#end
public static macro function exp(kissCode:ExprOf<String>) {
var pos = kissCode.pos;
var kissCode = ExprTools.getValue(kissCode);
/*
var lineNumber =
var pos = {
file: pos.file,
absoluteChar: pos.min,
line: lineNumber;
column:
};
*/
return _try(() -> {
var exp = null;
var stream = Stream.fromString(kissCode/*, pos*/);
var k = defaultKissState();
Reader.readAndProcess(stream, k, (nextExp) -> {
if (exp == null) {
exp = readerExpToHaxeExpr(nextExp, k);
} else {
throw KissError.fromExp(nextExp, "can't have multiple top-level expressions in Kiss.exp() input");
}
});
return exp;
});
}
#if macro
/**
Build macro: add fields to a class from a corresponding .kiss file
**/
@@ -535,5 +565,5 @@ class Kiss {
public static function convert(k:KissState, exp:ReaderExp) {
return readerExpToHaxeExpr(exp, k);
}
#end
}
#end

View File

@@ -50,8 +50,18 @@ class Stream {
}
#end
public static function fromString(content:String) {
return new Stream("string", content);
public static function fromString(content:String, position:Position = null) {
var file = "string";
if (position != null) {
file = position.file;
}
var s = new Stream(file, content);
if (position != null) {
s.line = position.line;
s.column = position.column;
s.absoluteChar = position.absoluteChar;
}
return s;
}
private function new(file:String, content:String) {

View File

@@ -6,6 +6,7 @@ import kiss.Prelude;
import kiss.List;
import kiss.Stream;
import haxe.ds.Option;
import kiss.Kiss;
using StringTools;
@@ -19,6 +20,10 @@ class BasicTestCase extends Test {
_testHaxeInsertion();
}
function testKissInsertion() {
Assert.equals(10, Kiss.exp('(+ 5 2 3)'));
}
function testStaticFunction() {
Assert.equals(6, BasicTestCase.myFloor(6.5));
}