Skip to content

Commit

Permalink
erweitert
Browse files Browse the repository at this point in the history
  • Loading branch information
maxymania committed Mar 17, 2017
1 parent 0540f4f commit 560f2c0
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ lib.a:
$(CC) -c $< -o $@

xpp += main.o
xpp += patlist.o strtest.o output.o tokenizer.o luab.o parser.c
xpp += patlist.o strtest.o output.o tokenizer.o luab.o parser.o buildmacro.o
xpp += lib.a lua.a

xpp: $(xpp)
Expand Down
Empty file removed Unbenanntes Dokument
Empty file.
69 changes: 69 additions & 0 deletions buildmacro.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2017 Simon Schmidt
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdarg.h>
#include "buildmacro.h"

sds bdmcr_build_macro(sds* args,int n,sds body){
int i;
sds dest = smust(sdsnew("local "));
for(i=0;i<n;++i){
if(i)dest = smust(sdscat(dest,","));
dest = smust(sdscatsds(dest,args[i]));
}
dest = smust(sdscat(dest," = ... ;\n"));
for(i=0;i<n;++i){
dest = smust(sdscatsds(dest,args[i]));
dest = smust(sdscat(dest," = normalize("));
dest = smust(sdscatsds(dest,args[i]));
dest = smust(sdscat(dest,");\n"));
}
dest = smust(sdscat(dest,"return solve("));
dest = smust(sdscatsds(dest,body));
dest = smust(sdscat(dest,");\n"));
return dest;
}

sds bdmcr_build_macro_stub(const char* body, ...){
const char* sp;
static sds margs[1024];
sds sbody,sresult;
va_list ap;
int i=0,l = 1024;

sbody = smust(sdsnew(body));

va_start(ap, body);
for(i=0;i<1024;++i){
sp = va_arg(ap, const char*);
if(!sp){ l = i; break; }
margs[i] = smust(sdsnew(sp));
}
va_end(ap);

sresult = bdmcr_build_macro(margs,l,sbody);
sdsclear(sbody);
for(i=0;i<l;++i){
sdsclear(margs[i]);
}
return sresult;
}

29 changes: 29 additions & 0 deletions buildmacro.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2017 Simon Schmidt
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once

#include "baselib.h"

sds bdmcr_build_macro(sds* args,int n,sds body);
sds bdmcr_build_macro_stub(const char* body, ...);
#define bdmcr_build_macro_c(...) bdmcr_build_macro_stub(__VA_ARGS__,(void*)0)

38 changes: 36 additions & 2 deletions luab.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,47 @@ static int solve(lua_State *L){
OutputStream_detroy(dest);
return 1;
}
static int normalize (lua_State *L){
int i,n;
n = lua_gettop(L);
for(i=1;i<=n;i++){
switch(lua_type(L,i)){
case LUA_TNIL:
case LUA_TBOOLEAN:
case LUA_TTABLE:
case LUA_TFUNCTION:
case LUA_TUSERDATA:
case LUA_TTHREAD:
case LUA_TLIGHTUSERDATA:
lua_pushliteral(L,"");
lua_replace(L,i);
break;
case LUA_TNUMBER:
lua_tostring(L,i);
break;
}
}
lua_settop(L,n);
return 1;
}

#define register_const(L,name) (lua_pushinteger(L, name),lua_setglobal(L, #name ))
lua_State* create_lua(){
lua_State *L = (lua_State*)ptrmust(luaL_newstate());
luaL_openlibs(L);
lua_settop(L,0);
lua_register(L,"solve",solve);
lua_register(L,"normalize",normalize);
lua_newtable(L);
lua_setglobal(L,"MACROS");
lua_newtable(L);
lua_setglobal(L,"MACROT");
luaL_loadstring(L, "function(n,f,t) MACROS[n]=f; MACROT[n]=t; return nil end");
//luaL_loadstring(L, "function(n,f,t) MACROS[n]=f; MACROT[n]=t; return nil end");
luaL_loadstring(L, "local n,f,t = ...; MACROS[n]=f; MACROT[n]=t; return nil");
lua_setglobal(L,"REGISTER");
register_const(L, MT_ARGS);
register_const(L, MT_BODY);
register_const(L, MT_SEMI);
return L;
}
sds luab_tosds(lua_State* L,int index){
Expand Down Expand Up @@ -103,7 +129,15 @@ void luab_setmacro(lua_State* L, const char* macro, const char* code,int type){
lua_getglobal(L,"MACROT");
lua_pushinteger(L, type);
lua_setfield(L,1,macro);
printf("gettop %d\n",lua_gettop(L));
lua_settop(L,0);
}
sds luab_eval(lua_State* L,sds code){
luaL_loadstring(L, code);
lua_pcall(L,0,1,0);
const char* c = lua_tostring(L,-1);
if(!c) c="";
sds ret = smust(sdsnew(c));
lua_settop(L,0);
return ret;
}

5 changes: 4 additions & 1 deletion luab.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
enum {
MT_ARGS = 1<<5,
MT_BODY = 1<<6,
MT_SEMI = 1<<7, /* ARGS: Semicolon instead of comma. */
};

lua_State* create_lua();
sds luab_tosds(lua_State* L,int index);
void luab_pushsds(lua_State* L,sds str);
void luab_call(lua_State* L);
int luab_getmacro(lua_State* L, sds macro);
void luab_setmacro(lua_State* L, const char* macro, const char* code,int type);
void luab_setmacro(lua_State* L, const char* macro, const char* code,int type);

sds luab_eval(lua_State* L,sds code);
20 changes: 14 additions & 6 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,32 @@
#include "luab.h"
#include "parser.h"
#include "luasrc/lauxlib.h"
#include "buildmacro.h"

int main(int argc,const char** argv){
struct tokenizer tok;
tok.buffer = smust(sdsempty());
tok.source = ptrmust(fopen("test.txt","r"));
lua_State* L = create_lua();

luab_setmacro(L,"tail_for","local pre,con,pos,body = ...; return solve(pre..';\\ndo {\\n'..body..'\\n'..pos..';\\n} while('..con..');')",MT_ARGS|MT_BODY);

OutputStream dest = OutputStream_new();
//OutputStream dest = FILE_asStream(stdout);
sds macro2 = bdmcr_build_macro_c(
"pre..';\\ndo {\\n'..body..'\\n'..pos..';\\n} while('..con..');'",
"body","pre","con","pos"
);

tokenizer_init();
luab_setmacro(L,"tail_for",macro2,MT_ARGS|MT_BODY|MT_SEMI);

sds d = smust(sdsnew("REGISTER"));
printf("%s\n",luab_eval(L,d));

//OutputStream dest = OutputStream_new();
OutputStream dest = FILE_asStream(stdout);

tokenizer_init();

parser_parse(&tok,L,dest);

printf("%s\n",(char*)dest->data);
//printf("%s\n",(char*)dest->data);
return 0;
}

Expand Down
46 changes: 44 additions & 2 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,46 @@ static void parser_parse_args(struct tokenizer *t,lua_State* L){
OutputStream_detroy(dest);
}

static void parser_parse_args_semicolon(struct tokenizer *t,lua_State* L){
OutputStream dest = OutputStream_new();
int c = 0;
sds s = 0;
for(;;){
s = tokenize_get(t,(OutputStream)0);
if(!s) {
fprintf(stderr,"unexpected EOF\n");
abort();
}
switch(*s){
case '(':
c++;
break;
case ')':
if(!c){
sdsclear(s);
goto end;
}
c--;
break;
case ';':
if(!c){
luab_pushsds(L,(sds)(dest->data) );
sdssetlen((sds)(dest->data),0);
*((sds)(dest->data)) = 0;
sdsclear(s);
continue;
}
break;
}
OutputStream_write(dest,s,sdslen(s));
OutputStream_write(dest," ",1);
sdsclear(s);
}
end:
luab_pushsds(L,(sds)(dest->data) );
OutputStream_detroy(dest);
}

static void parser_parse_body(struct tokenizer *t,lua_State* L){
OutputStream dest = OutputStream_new();
int c = 0;
Expand Down Expand Up @@ -95,7 +135,7 @@ static void parser_parse_body(struct tokenizer *t,lua_State* L){

void parser_parse(struct tokenizer *t,lua_State* L,OutputStream dest){
sds s;
int i;
int i,N;
for(;;){
s = tokenize_get(t,dest);

Expand All @@ -118,7 +158,8 @@ void parser_parse(struct tokenizer *t,lua_State* L,OutputStream dest){
abort();
}
sdsclear(s);
parser_parse_args(t,L);
if(i&MT_SEMI) parser_parse_args_semicolon(t,L);
else parser_parse_args(t,L);
}
if(i&MT_BODY){
s = tokenize_get(t,dest);
Expand All @@ -132,6 +173,7 @@ void parser_parse(struct tokenizer *t,lua_State* L,OutputStream dest){
}
sdsclear(s);
parser_parse_body(t,L);
lua_insert(L,2);
}

luab_call(L);
Expand Down
7 changes: 3 additions & 4 deletions test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
int main(int argc,const char** argv){
int i,j;
/* for(i=0;i<argc;++i) */
tail_for(i=0,i<argc,++i) {
tail_for(j=0,j<10,++j) {
//tail_for(i=0;i<argc;++i)
tail_for(i=0;i<argc;++i) {
tail_for(j=0;j<10;++j) {
printf("%s\n",argv[i]);
}
}
}


1 change: 1 addition & 0 deletions tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ static int tokenizer_refill(struct tokenizer *t,int force){
t->buffer = smust(sdscat(t->buffer,data));
return 2;
}
#define LINEP printf("\n%d\n",__LINE__);
sds tokenize_get(struct tokenizer *t, OutputStream targ){
const char* eoc;
size_t skp;
Expand Down

0 comments on commit 560f2c0

Please sign in to comment.