Имплементиран quote навод оператер

This commit is contained in:
kappa 2019-01-19 20:30:52 +01:00
parent 508e5fc9e1
commit cee90aee74
4 changed files with 43 additions and 22 deletions

48
eval.c
View file

@ -14,17 +14,16 @@ object eval(object input)
if (TYPE(input) == nilObject || TYPE(input) == numberObject || if (TYPE(input) == nilObject || TYPE(input) == numberObject ||
TYPE(input) == errorObject) TYPE(input) == errorObject)
{ {
result = input; result = copyObject(input);
} }
else if (TYPE(input) == symbolObject) else if (TYPE(input) == symbolObject)
{ {
if (symbolExists(SYM(input))) if (symbolExists(SYM(input)))
{ {
result = input; result = copyObject(input);
} }
else else
{ {
deleteObject(input);
TYPE(result) = errorObject; TYPE(result) = errorObject;
ERR(result) = unrecognizedSymbolError; ERR(result) = unrecognizedSymbolError;
} }
@ -33,34 +32,39 @@ object eval(object input)
{ {
if (!properList(input)) if (!properList(input))
{ {
deleteObject(input);
TYPE(result) = errorObject; TYPE(result) = errorObject;
ERR(result) = improperListError; ERR(result) = improperListError;
} }
else
object *currentCell = &input;
int noErrors = 1;
while (TYPE(*currentCell) != nilObject)
{ {
CAR(*currentCell) = eval(CAR(*currentCell)); object *currentCell = &input;
int noErrors = 1;
if (TYPE(CAR(*currentCell)) == errorObject) while (TYPE(*currentCell) != nilObject)
{ {
noErrors = 0; if (strcmp(SYM(CAR(input)), "навод") != 0)
TYPE(result) = errorObject; {
ERR(result) = ERR(CAR(*currentCell)); CAR(*currentCell) =
break; eval(CAR(*currentCell));
} }
currentCell = &CDR(*currentCell);
}
if (noErrors) if (TYPE(CAR(*currentCell)) == errorObject)
{ {
result = apply(CAR(input), CDR(input)); noErrors = 0;
TYPE(result) = errorObject;
ERR(result) = ERR(CAR(*currentCell));
break;
}
currentCell = &CDR(*currentCell);
}
if (noErrors)
{
result = apply(CAR(input), CDR(input));
}
} }
deleteObject(input);
} }
deleteObject(input);
return result; return result;
} }

1
init.c
View file

@ -20,4 +20,5 @@ void init()
addSymbolInternal("-", &subtract); addSymbolInternal("-", &subtract);
addSymbolInternal("*", &multiply); addSymbolInternal("*", &multiply);
addSymbolInternal("/", &divide); addSymbolInternal("/", &divide);
addSymbolInternal("навод", &quote);
} }

View file

@ -135,3 +135,18 @@ object divide(object parameters)
return result; return result;
} }
object quote(object parameters)
{
object result;
if (listLength(parameters) != 1)
{
TYPE(result) = errorObject;
ERR(result) = argumentNumberError;
}
else
{
result = copyObject(CAR(parameters));
}
return result;
}

View file

@ -4,3 +4,4 @@ object add(object parameters);
object subtract(object parameters); object subtract(object parameters);
object multiply(object parameters); object multiply(object parameters);
object divide(object parameters); object divide(object parameters);
object quote(object parameters);