Browse Source

Considerable updates to Kuroko

K. Lange 1 year ago
parent
commit
67e8c539cb
7 changed files with 49 additions and 48 deletions
  1. 1 0
      .gitignore
  2. 4 3
      Makefile
  3. 37 39
      bim.c
  4. 1 1
      kuroko
  5. 2 2
      syntax/bash.krk
  6. 1 1
      syntax/c.krk
  7. 3 2
      syntax/krk.krk

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
 bim
 *.o
 *.swp
+*.so

+ 4 - 3
Makefile

@@ -1,6 +1,6 @@
 TARGET=bim
 CFLAGS=-g -flto -std=c99 -Wvla -pedantic -Wall -Wextra -I. $(shell bash docs/git-tag) -Wno-unused-parameter -DNO_SYSTEM_BINDS
-LDFLAGS=-Lkuroko -Wl,-rpath -Wl,kuroko
+LDFLAGS=-Wl,-rpath -Wl,'$$ORIGIN' -L.
 LDLIBS=-lkuroko -ldl
 
 prefix=/usr/local
@@ -14,7 +14,7 @@ INSTALL_PROGRAM=$(INSTALL)
 INSTALL_DATA=$(INSTALL) -m 644
 
 SYNTAXES = $(patsubst %.c, %.o, $(sort $(wildcard syntax/*.c)))
-KUROKO = kuroko/libkuroko.so
+KUROKO = libkuroko.so
 HEADERS = $(wildcard bim-*.h)
 
 .PHONY: all clean distclean install install-strip uninstall
@@ -26,8 +26,9 @@ syntax/*.o: $(HEADERS)
 
 bim: bim.o $(SYNTAXES) $(KUROKO)
 
-kuroko/libkuroko.so: kuroko/*.c kuroko/*.h
+libkuroko.so: kuroko/src/*.c kuroko/src/**.h
 	$(MAKE) -C kuroko
+	cp kuroko/libkuroko.so ./
 
 clean:
 	-rm -f $(TARGET) bim.o $(SYNTAXES)

+ 37 - 39
bim.c

@@ -16,9 +16,9 @@
  */
 #include "bim-core.h"
 #include "bim-syntax.h"
-#include "kuroko/kuroko.h"
-#include "kuroko/vm.h"
-#include "kuroko/debug.h"
+#include "kuroko/src/kuroko.h"
+#include "kuroko/src/vm.h"
+#include "kuroko/src/debug.h"
 
 global_config_t global_config = {
 	/* State */
@@ -10424,12 +10424,19 @@ void load_bimrc(void) {
 #define str(s) #s
 
 static KrkClass * syntaxStateClass = NULL;
+
+struct SyntaxState {
+	KrkInstance inst;
+	struct syntax_state * state;
+};
+
+
 static int syn_krk(struct syntax_state * state) {
 	/* Assume env->syntax is us... */
 	void * krkFunc = env->syntax->krkFunc;
 	KrkInstance * s = krk_newInstance(syntaxStateClass); /* TODO state class */
 	krk_push(OBJECT_VAL(s));
-	s->_internal = state;
+	((struct SyntaxState*)s)->state = state;
 
 	int key = 0;
 
@@ -10501,24 +10508,26 @@ int c_keyword_qualifier(int c) {
 	return isalnum(c) || (c == '_');
 }
 
+#define BIM_STATE() \
+	if (argc < 1) return krk_runtimeError(vm.exceptions.typeError, "expected state"); \
+	KrkInstance * _self = AS_INSTANCE(argv[0]); \
+	struct SyntaxState * self = (struct SyntaxState*)_self; \
+	struct syntax_state * state = self->state;
+
 static KrkValue bim_krk_state_getstate(int argc, KrkValue argv[]) {
-	KrkInstance * self = AS_INSTANCE(argv[0]);
-	struct syntax_state * state = self->_internal;
+	BIM_STATE();
 	return INTEGER_VAL(state->state);
 }
 static KrkValue bim_krk_state_index(int argc, KrkValue argv[]) {
-	KrkInstance * self = AS_INSTANCE(argv[0]);
-	struct syntax_state * state = self->_internal;
+	BIM_STATE();
 	return INTEGER_VAL(state->i);
 }
 static KrkValue bim_krk_state_lineno(int argc, KrkValue argv[]) {
-	KrkInstance * self = AS_INSTANCE(argv[0]);
-	struct syntax_state * state = self->_internal;
+	BIM_STATE();
 	return INTEGER_VAL(state->line_no);
 }
 static KrkValue bim_krk_state_charat(int argc, KrkValue argv[]) {
-	KrkInstance * self = AS_INSTANCE(argv[0]);
-	struct syntax_state * state = self->_internal;
+	BIM_STATE();
 	int charAt = charat();
 	if (charAt == -1) return NONE_VAL();
 	char tmp[8] = {0};
@@ -10526,8 +10535,7 @@ static KrkValue bim_krk_state_charat(int argc, KrkValue argv[]) {
 	return OBJECT_VAL(krk_copyString(tmp,len));
 }
 static KrkValue bim_krk_state_nextchar(int argc, KrkValue argv[]) {
-	KrkInstance * self = AS_INSTANCE(argv[0]);
-	struct syntax_state * state = self->_internal;
+	BIM_STATE();
 	int nextChar = nextchar();
 	if (nextChar == -1) return NONE_VAL();
 	char tmp[8] = {0};
@@ -10535,8 +10543,7 @@ static KrkValue bim_krk_state_nextchar(int argc, KrkValue argv[]) {
 	return OBJECT_VAL(krk_copyString(tmp,len));
 }
 static KrkValue bim_krk_state_lastchar(int argc, KrkValue argv[]) {
-	KrkInstance * self = AS_INSTANCE(argv[0]);
-	struct syntax_state * state = self->_internal;
+	BIM_STATE();
 	int lastChar = lastchar();
 	if (lastChar == -1) return NONE_VAL();
 	char tmp[8] = {0};
@@ -10544,8 +10551,7 @@ static KrkValue bim_krk_state_lastchar(int argc, KrkValue argv[]) {
 	return OBJECT_VAL(krk_copyString(tmp,len));
 }
 static KrkValue bim_krk_state_charrel(int argc, KrkValue argv[]) {
-	KrkInstance * self = AS_INSTANCE(argv[0]);
-	struct syntax_state * state = self->_internal;
+	BIM_STATE();
 	long arg = AS_INTEGER(argv[1]);
 	int charRel = charrel(arg);
 	if (charRel == -1) return NONE_VAL();
@@ -10573,8 +10579,7 @@ static KrkValue bim_krk_state_isxdigit(int argc, KrkValue argv[]) {
 	return BOOLEAN_VAL(!!isxdigit(c));
 }
 static KrkValue bim_krk_state_paint(int argc, KrkValue argv[]) {
-	KrkInstance * self = AS_INSTANCE(argv[0]);
-	struct syntax_state * state = self->_internal;
+	BIM_STATE();
 	long howMuch = AS_INTEGER(argv[1]);
 	if (howMuch == -1) howMuch = state->line->actual;
 	long whatFlag = AS_INTEGER(argv[2]);
@@ -10582,14 +10587,12 @@ static KrkValue bim_krk_state_paint(int argc, KrkValue argv[]) {
 	return NONE_VAL();
 }
 static KrkValue bim_krk_state_paintComment(int argc, KrkValue argv[]) {
-	KrkInstance * self = AS_INSTANCE(argv[0]);
-	struct syntax_state * state = self->_internal;
+	BIM_STATE();
 	paint_comment(state);
 	return NONE_VAL();
 }
 static KrkValue bim_krk_state_skip(int argc, KrkValue argv[]) {
-	KrkInstance * self = AS_INSTANCE(argv[0]);
-	struct syntax_state * state = self->_internal;
+	BIM_STATE();
 	skip();
 	return NONE_VAL();
 }
@@ -10599,16 +10602,14 @@ static KrkValue bim_krk_state_cKeywordQualifier(int argc, KrkValue argv[]) {
 	return BOOLEAN_VAL(!!c_keyword_qualifier(AS_CSTRING(argv[1])[0]));
 }
 static KrkValue bim_krk_state_findKeywords(int argc, KrkValue argv[]) {
-	KrkInstance * self = AS_INSTANCE(argv[0]);
-	struct syntax_state * state = self->_internal;
+	BIM_STATE();
 	KrkValue qualifier = argv[3];
 	if (IS_BOUND_METHOD(qualifier) && AS_BOUND_METHOD(qualifier)->method->type == OBJ_NATIVE
 		&& ((KrkNative*)AS_BOUND_METHOD(qualifier)->method)->function == bim_krk_state_cKeywordQualifier) {
-		KrkValue _list_internal = OBJECT_VAL(AS_INSTANCE(argv[1])->_internal);
-		char ** keywordsAsCArray = malloc(sizeof(char*) * (AS_LIST(_list_internal)->count+1));
-		keywordsAsCArray[AS_LIST(_list_internal)->count] = NULL;
-		for (size_t i = 0; i < AS_LIST(_list_internal)->count; ++i) {
-			keywordsAsCArray[i] = AS_CSTRING(AS_LIST(_list_internal)->values[i]);
+		char ** keywordsAsCArray = malloc(sizeof(char*) * (AS_LIST(argv[1])->count+1));
+		keywordsAsCArray[AS_LIST(argv[1])->count] = NULL;
+		for (size_t i = 0; i < AS_LIST(argv[1])->count; ++i) {
+			keywordsAsCArray[i] = AS_CSTRING(AS_LIST(argv[1])->values[i]);
 			/* TODO type check */
 		}
 		long flag = AS_INTEGER(argv[2]);
@@ -10620,8 +10621,7 @@ static KrkValue bim_krk_state_findKeywords(int argc, KrkValue argv[]) {
 	return BOOLEAN_VAL(0);
 }
 static KrkValue bim_krk_state_matchAndPaint(int argc, KrkValue argv[]) {
-	KrkInstance * self = AS_INSTANCE(argv[0]);
-	struct syntax_state * state = self->_internal;
+	BIM_STATE();
 	KrkValue qualifier = argv[3];
 	if (IS_BOUND_METHOD(qualifier) && AS_BOUND_METHOD(qualifier)->method->type == OBJ_NATIVE
 		&& ((KrkNative*)AS_BOUND_METHOD(qualifier)->method)->function == bim_krk_state_cKeywordQualifier) {
@@ -10634,18 +10634,15 @@ static KrkValue bim_krk_state_matchAndPaint(int argc, KrkValue argv[]) {
 	return BOOLEAN_VAL(0);
 }
 static KrkValue bim_krk_state_rewind(int argc, KrkValue argv[]) {
-	KrkInstance * self = AS_INSTANCE(argv[0]);
-	struct syntax_state * state = self->_internal;
+	BIM_STATE();
 	state->i -= AS_INTEGER(argv[1]);
 	return NONE_VAL();
 }
 static KrkValue bim_krk_state_commentBuzzwords(int argc, KrkValue argv[]) {
-	KrkInstance * self = AS_INSTANCE(argv[0]);
-	struct syntax_state * state = self->_internal;
+	BIM_STATE();
 	return BOOLEAN_VAL(common_comment_buzzwords(state));
 }
 
-
 const char bimBuiltins_krk[] =
 	"let handlers = {}\n"
 	"def highlighter(name,extensions=(),spaces=False):\n"
@@ -10721,7 +10718,8 @@ void initialize(void) {
 
 	KrkString * strSyntaxState = S("SyntaxState");
 	krk_push(OBJECT_VAL(strSyntaxState));
-	syntaxStateClass = krk_newClass(strSyntaxState);
+	syntaxStateClass = krk_newClass(strSyntaxState, vm.objectClass);
+	syntaxStateClass->allocSize = sizeof(struct SyntaxState);
 	krk_attachNamedObject(&AS_INSTANCE(bimModule)->fields, "SyntaxState", (KrkObj*)syntaxStateClass);
 	krk_pop(); /* strSyntaxState */
 	krk_defineNative(&syntaxStateClass->methods, ":state", bim_krk_state_getstate);

+ 1 - 1
kuroko

@@ -1 +1 @@
-Subproject commit bd644026b00345525e74b8f1b0f9521307c1aab4
+Subproject commit 50e490217070f9dbe085bc57637b12085b14b713

+ 2 - 2
syntax/bash.krk

@@ -111,13 +111,13 @@ def syntax_bash(state):
             state.paint(1, flags.FLAG_KEYWORD)
             return 0
         else if state.cKeywordQualifier(state.charat):
-            for i = 0, state.charrel(i) != None, i++:
+            for i = 0; state.charrel(i) != None; i++:
                 if state.charrel(i) == ' ': break
                 if state.charrel(i) == '=':
                     state.paint(i, flags.FLAG_TYPE)
                     state.skip()
                     return 0
-            for i = 0, state.charrel(i) != None, i++:
+            for i = 0; state.charrel(i) != None; i++:
                 if state.charrel(i) == '(':
                     state.paint(i, flags.FLAG_TYPE)
                     return 0

+ 1 - 1
syntax/c.krk

@@ -154,7 +154,7 @@ def syntax_c(state):
                     state.paint(1, flags.FLAG_STRING)
                     while state.charat != '>' and state.charat != None:
                         state.paint(1, flags.FLAG_STRING)
-                    if state.charat != NONE:
+                    if state.charat != None:
                         state.paint(1, flags.FLAG_STRING)
             else if state.matchAndPaint("if", flags.FLAG_PRAGMA, state.cKeywordQualifier):
                 if state.charat == ' ' and state.nextchar == '0' and state.charrel(2) == None:

+ 3 - 2
syntax/krk.krk

@@ -3,12 +3,13 @@ from bim import highlighter, flags
 let keywords = [
     'and','class','def','else','export','for','if','in','import','let','not',
     'or','return','while','try','except','raise','continue','break','as','from',
-    'elif', 'lambda'
+    'elif', 'lambda', 'pass', 'with', 'is', 'del'
 ]
 
 let types = [
     'self','super','len','str','int','float','dir','repr','list','dict','range',
-    'object','exception','isinstance','type','print'
+    'object','exception','isinstance','type','print','tuple','bool','any','all',
+    'hex','ord','chr'
 ]
 
 let special = [