Преглед на файлове

Add Symbol factory

pull/21/head
mtardy преди 4 години
родител
ревизия
4db744d537
променени са 2 файла, в които са добавени 57 реда и са изтрити 1 реда
  1. +20
    -1
      object.c
  2. +37
    -0
      parse.leg

+ 20
- 1
object.c Целия файл

@ -249,7 +249,7 @@ oop makeString(char *value)
// value will be used directly
oop makeStringFrom(char *value, size_t l)
{
oop newString = malloc(sizeof(union object));
oop newString = malloc(sizeof(struct String));
newString->type = String;
newString->String.value = value;
newString->String.size = l;
@ -318,6 +318,25 @@ oop makeSymbol(char *name)
return newSymb;
}
oop makeSymbolFrom(char *name)
{
oop newSymbol= malloc(sizeof(struct Symbol));
newSymbol->type= Symbol;
newSymbol->Symbol.name= name;
newSymbol->Symbol.prototype= 0;
return newSymbol;
}
oop makeSymbolFromChar(char c, int repeat)
{
char *str= malloc(sizeof(char) * (repeat + 1));
for (int i=0; i<repeat; ++i) {
str[i]= c;
}
str[repeat]= '\0';
return makeSymbolFrom(str);
}
oop makeFunction(primitive_t primitive, oop name, oop param, oop body, oop parentScope, oop fixed)
{
oop newFunc = malloc(sizeof(struct Function));

+ 37
- 0
parse.leg Целия файл

@ -1983,6 +1983,42 @@ oop prim_String(oop scope, oop params)
return null;
}
oop prim_Symbol(oop scope, oop params)
{
if (!map_hasIntegerKey(params, 0)) return null;
oop arg= get(params, Map, elements)[0].value;
switch (getType(arg)) {
case Integer: {
if (map_hasIntegerKey(params, 1)) {
int repeat= getInteger(arg);
char c= getInteger(get(params, Map, elements)[1].value);
return makeSymbolFromChar(c, repeat);
}
// ???????????????????????????
//char *ok= get(arg, String, value);
//printf("%s\n", ok);
break;
}
case String: {
return makeSymbol(get(arg, String, value));
}
case Map: {
if (map_isArray(arg)) {
size_t len= map_size(arg);
char *str= malloc(sizeof(char) * len + 1);
for (size_t i=0; i < len; ++i) {
str[i]= getInteger(get(arg, Map, elements)[i].value);
}
return makeSymbolFrom(str);
}
}
case Symbol: {
return arg;
}
}
return null;
}
oop prim_Integer(oop scope, oop params)
{
if (!map_hasIntegerKey(params, 0)) return null;
@ -2085,6 +2121,7 @@ int main(int argc, char **argv)
map_set(globals, intern("String" ), makeFunction(prim_String , intern("String" ), null, null, globals, null));
map_set(globals, intern("Integer" ), makeFunction(prim_Integer , intern("Integer" ), null, null, globals, null));
map_set(globals, intern("Map" ), makeFunction(prim_Map , intern("Map" ), null, null, globals, null));
map_set(globals, intern("Symbol" ), makeFunction(prim_Symbol , intern("Symbol" ), null, null, globals, null));
map_set(globals, intern("scope"), makeFunction(prim_scope, intern("scope"), null, null, globals, null));

Зареждане…
Отказ
Запис