[gatest] Parse division and dual operations

This commit is contained in:
Bill Currie 2023-05-29 09:52:10 +09:00
parent 4e441d359a
commit 681629a5d1
3 changed files with 26 additions and 10 deletions

View file

@ -117,7 +117,7 @@ typedef enum {
OPENS, CLOSES,
MUL, DIV, PLUS, MINUS,
WEDGE, ANTIWEDGE, DOT,
REVERSE,
REVERSE, DUAL,
} token_e;
script_t script;
@ -157,6 +157,7 @@ get_token ()
case "&": return {ANTIWEDGE, nil};
case ".": return {DOT, nil};
case "~": return {REVERSE, nil};
case "!": return {DUAL, nil};
}
return {ID, .name = token_str };
}
@ -164,7 +165,8 @@ get_token ()
static void
syntax_error ()
{
obj_error (nil, 0, "syntax error: %d\n", Script_GetLine (script));
obj_error (nil, 0, "syntax error before `%s': %d\n", token_str,
Script_GetLine (script));
}
static int
@ -206,10 +208,13 @@ factor ()
MultiVector *vec;
if (match (REVERSE)) {
advance ();
vec = [expression () reverse];
vec = [factor () reverse];
} else if (match (DUAL)) {
advance ();
vec = [factor () dual];
} else if (match (MINUS)) {
advance ();
vec = [minus_one product:expression ()];
vec = [minus_one product:factor ()];
} else if (match (OPENP)) {
advance ();
vec = expression ();
@ -289,10 +294,11 @@ term ()
} else {
vec = high_term ();
}
if (match (REVERSE) || match (OPENP) || match (ID)) {
if (match (REVERSE) || match (DUAL) || match (OPENP) || match (ID)) {
op = @selector(product:);
// } else if (match (DIV)) {
// advance ();
} else if (match (DIV)) {
op = @selector(divide:);
advance ();
} else {
return vec;
}
@ -362,7 +368,7 @@ static int
parse_script (string name, QFile file)
{
script = Script_New ();
Script_SetSingle (script, "()[]{}/+-^&.~=;");
Script_SetSingle (script, "()[]{}/+-^&.~=;!");
token_str = Script_FromFile (script, name, file);
while (!match (EOF)) {

View file

@ -26,6 +26,7 @@
-(double *) componentFor:(BasisBlade *) blade;
-(MultiVector *) product:(MultiVector *) rhs;
-(MultiVector *) divide:(MultiVector *) rhs;
-(MultiVector *) wedge:(MultiVector *) rhs;
-(MultiVector *) dot:(MultiVector *) rhs;
-(MultiVector *) plus:(MultiVector *) rhs;

View file

@ -131,6 +131,17 @@ static MultiVector *new_mv (Algebra *algebra, BasisLayout *layout)
return prod;
}
-(MultiVector *) divide:(MultiVector *) rhs
{
MultiVector *sqr = [rhs product:rhs];
double smag = sqr.components[[sqr indexFor:0]];
MultiVector *div = [self product:rhs];
for (int i = 0; i < div.num_components; i++) {
div.components[i] /= smag;
}
return div;
}
-(MultiVector *) wedge:(MultiVector *) rhs
{
MultiVector *prod = new_mv (algebra, nil);
@ -246,7 +257,6 @@ static MultiVector *new_mv (Algebra *algebra, BasisLayout *layout)
{
MultiVector *dual = new_mv (algebra, nil);
unsigned dual_mask = (1 << [algebra dimension]) - 1;
printf ("dual: %x %d\n", dual_mask, [algebra dimension]);
for (int i = 0; i < num_components; i++) {
if (!components[i]) {
continue;
@ -256,7 +266,6 @@ static MultiVector *new_mv (Algebra *algebra, BasisLayout *layout)
unsigned mask = [lb mask] ^ dual_mask;
double s = 1;
int ind = [layout bladeIndex:mask];
printf (" : %x %d\n", mask, ind);
dual.components[ind] += s * lc;
}
return dual;