Skip to content

Commit

Permalink
added straggler xnor
Browse files Browse the repository at this point in the history
  • Loading branch information
billhails committed Aug 13, 2024
1 parent 9108c41 commit 0fa359a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/lambda_conversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,16 @@ static LamExp *makeLamAnd(LamList *args, LamContext *env) {
return result;
}

static LamExp *makeLamXnor(LamList *args) {
LamExp *xor = makeBinOp(LAMPRIMOP_TYPE_XOR, args);
int save = PROTECT(xor);
LamUnaryApp *not = newLamUnaryApp(CPI(args), LAMUNARYOP_TYPE_NOT, xor);
PROTECT(not);
LamExp *exp = newLamExp_Unary(CPI(not), not);
UNPROTECT(save);
return exp;
}

static LamExp *makeLamNand(LamList *args, LamContext *env) {
// (nand a b) => (not (and a b))
LamExp *and = makeLamAnd(args, env);
Expand Down Expand Up @@ -787,6 +797,8 @@ static LamExp *makePrimApp(HashSymbol *symbol, LamList *args, LamContext *env) {
return makeLamNor(args, env);
if (symbol == xorSymbol())
return makeBinOp(LAMPRIMOP_TYPE_XOR, args);
if (symbol == xnorSymbol())
return makeLamXnor(args);
if (symbol == eqSymbol())
return makeBinOp(LAMPRIMOP_TYPE_EQ, args);
if (symbol == neSymbol())
Expand Down
1 change: 1 addition & 0 deletions src/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ struct PmModule *mod = yyextra;
"or" { return OR; }
"nor" { return NOR; }
"xor" { return XOR; }
"xnor" { return XNOR; }
"print" { return PRINT; }
"_" { return WILDCARD; }
"switch" { return SWITCH; }
Expand Down
3 changes: 2 additions & 1 deletion src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ static AstArg *makeAstLookupArg(PmModule *mod, HashSymbol *nsName, HashSymbol *s

%right ARROW
%right THEN
%left AND OR XOR NAND NOR
%left AND OR XOR NAND NOR XNOR
%nonassoc NOT
%nonassoc EQ NE GT LT GE LE
%nonassoc CMP
Expand Down Expand Up @@ -797,6 +797,7 @@ binop : expression THEN expression { $$ = binOpToFunCall(mod, thenSymbol(),
| expression AND expression { $$ = binOpToFunCall(mod, andSymbol(), $1, $3); }
| expression OR expression { $$ = binOpToFunCall(mod, orSymbol(), $1, $3); }
| expression XOR expression { $$ = binOpToFunCall(mod, xorSymbol(), $1, $3); }
| expression XNOR expression { $$ = binOpToFunCall(mod, xnorSymbol(), $1, $3); }
| expression NOR expression { $$ = binOpToFunCall(mod, norSymbol(), $1, $3); }
| expression NAND expression { $$ = binOpToFunCall(mod, nandSymbol(), $1, $3); }
| expression EQ expression { $$ = binOpToFunCall(mod, eqSymbol(), $1, $3); }
Expand Down
8 changes: 8 additions & 0 deletions src/symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ HashSymbol *xorSymbol() {
return res;
}

HashSymbol *xnorSymbol() {
static HashSymbol *res = NULL;
if (res == NULL) {
res = newSymbol("xnor");
}
return res;
}

HashSymbol *eqSymbol() {
static HashSymbol *res = NULL;
if (res == NULL) {
Expand Down
1 change: 1 addition & 0 deletions src/symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ HashSymbol *subSymbol(void);
HashSymbol *thenSymbol(void);
HashSymbol *trueSymbol(void);
HashSymbol *xorSymbol(void);
HashSymbol *xnorSymbol(void);

#endif
5 changes: 5 additions & 0 deletions tests/fn/test_logic.fn
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ assert((true xor false) == true);
assert((false xor true) == true);
assert((false xor false) == false);

assert((true xnor true) == true);
assert((true xnor false) == false);
assert((false xnor true) == false);
assert((false xnor false) == true);

assert((true nand true) == false);
assert((true nand false) == true);
assert((false nand true) == true);
Expand Down
2 changes: 1 addition & 1 deletion vim/syntax/fnatural.vim
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ syntax match fnOperator "\v\!\="
syntax match fnOperator "\v\<"
syntax match fnOperator "\v\>"
syntax match fnOperator "\v\="
syntax keyword fnOperator and nand or nor not xor
syntax keyword fnOperator and nand or nor not xor xnor
highlight link fnOperator Operator

syntax region fnString start=/\v'/ skip=/\v\\./ end=/\v'/
Expand Down

0 comments on commit 0fa359a

Please sign in to comment.