Skip to content

Commit

Permalink
Merge pull request #120 from billhails/macros
Browse files Browse the repository at this point in the history
Macros
  • Loading branch information
billhails authored Oct 12, 2024
2 parents ef6d9ba + 2320d7a commit 74ef689
Show file tree
Hide file tree
Showing 27 changed files with 928 additions and 471 deletions.
4 changes: 4 additions & 0 deletions docs/generated/ast.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ AstAlias --name--> HashSymbol
AstAlias --type--> AstType
AstExprAlias --name--> HashSymbol
AstExprAlias --value--> AstExpression
AstDefMacro --name--> HashSymbol
AstDefMacro --definition--> AstAltFunction
AstTypeDef --userType--> AstUserType
AstTypeDef --typeBody--> AstTypeBody
AstUserType --symbol--> HashSymbol
Expand Down Expand Up @@ -90,6 +92,7 @@ AstLookupOrSymbol --symbol--> HashSymbol
AstLookupOrSymbol --lookup--> AstLookupSymbol
AstDefinition --define--> AstDefine
AstDefinition --typeDef--> AstTypeDef
AstDefinition --macro--> AstDefMacro
AstDefinition --alias--> AstAlias
AstDefinition --blank--> void_ptr
AstTypeClause --integer--> void_ptr
Expand All @@ -112,6 +115,7 @@ AstExpression --alias--> AstExprAlias
AstExpression --funCall--> AstFunCall
AstExpression --lookup--> AstLookup
AstExpression --symbol--> HashSymbol
AstExpression --gensym--> HashSymbol
AstExpression --number--> MaybeBigInt
AstExpression --character--> char
AstExpression --fun--> AstCompositeFunction
Expand Down
7 changes: 7 additions & 0 deletions docs/generated/lambda.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ Plain lambda structures generated by lambda conversion.

```mermaid
flowchart TD
LamMacroTable --entries--> entries
LamGenSymTable --entries--> entries
LamInfoTable --entries--> entries
LamAliasTable --entries--> entries
LamExpTable --entries--> entries
LamLam --args--> LamVarList
LamLam --exp--> LamExp
LamMacro --args--> LamVarList
LamMacro --exp--> LamExp
LamMacro --env--> LamContext
LamVarList --var--> HashSymbol
LamVarList --next--> LamVarList
LamPrimApp --type--> LamPrimOp
Expand Down Expand Up @@ -73,6 +78,8 @@ LamLetRecBindings --val--> LamExp
LamLetRecBindings --next--> LamLetRecBindings
LamContext --frame--> LamInfoTable
LamContext --aliases--> LamAliasTable
LamContext --gensyms--> LamGenSymTable
LamContext --macros--> LamMacroTable
LamContext --parent--> LamContext
LamAmb --left--> LamExp
LamAmb --right--> LamExp
Expand Down
36 changes: 36 additions & 0 deletions fn/macro.fn
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
let
fn NOT {
(true) { false }
(false) { true }
}
prefix 40 "NOT" NOT;

macro AND(a, b) { if (a) { b } else { false } }
infix left 30 "AND" AND;

macro OR(a, b) { if (a) { true } else { b } }
infix left 30 "OR" OR;

fn XOR {
(true, true) { false }
(true, false) { true }
(false, true) { true }
(false, false) { false }
}
infix left 30 "XOR" XOR;

macro NAND(a, b) { NOT (a AND b) }
infix left 30 "NAND" NAND;

macro NOR(a, b) { NOT (a OR b) }
infix left 30 "NOR" NOR;

macro XNOR(a, b) { NOT (a XOR b) }
infix left 30 "XNOR" XNOR;

fn a() { print("a called"); false }
fn b() { print("b called"); false }
fn c() { print("c called"); true }
in
print(a() AND b() OR c());
print(a() AND (b() OR c()));
1 change: 1 addition & 0 deletions src/anf_normalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,7 @@ static bool lamExpIsLambda(LamExp *val) {
case LAMEXP_TYPE_COND:
case LAMEXP_TYPE_MAKEVEC:
case LAMEXP_TYPE_LOOKUP:
case LAMEXP_TYPE_MAKE_TUPLE:
return false;
case LAMEXP_TYPE_COND_DEFAULT:
cant_happen("lamExpIsLambda encountered cond default");
Expand Down
6 changes: 6 additions & 0 deletions src/ast.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ structs:
name: HashSymbol
value: AstExpression

AstDefMacro:
name: HashSymbol
definition: AstAltFunction

AstTypeDef:
userType: AstUserType
typeBody: AstTypeBody
Expand Down Expand Up @@ -192,6 +196,7 @@ unions:
AstDefinition:
define: AstDefine
typeDef: AstTypeDef
macro: AstDefMacro
alias: AstAlias
blank: void_ptr

Expand Down Expand Up @@ -220,6 +225,7 @@ unions:
funCall: AstFunCall
lookup: AstLookup
symbol: HashSymbol
gensym: HashSymbol
number: MaybeBigInt
character: char
fun: AstCompositeFunction
Expand Down
13 changes: 13 additions & 0 deletions src/ast_pp.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "file_id.h"
#include "ast_pp.h"

static void ppAstDefMacro(PrattUTF8 *, AstDefMacro *);
static void ppAstDefinitions(PrattUTF8 *, AstDefinitions *);
static void ppAstDefinition(PrattUTF8 *, AstDefinition *);
static void ppAstDefine(PrattUTF8 *, AstDefine *);
Expand Down Expand Up @@ -126,11 +127,23 @@ static void ppAstDefinition(PrattUTF8 *dest, AstDefinition *definition) {
break;
case AST_DEFINITION_TYPE_BLANK:
break;
case AST_DEFINITION_TYPE_MACRO:
ppAstDefMacro(dest, definition->val.macro);
break;
default:
cant_happen("unrecognised %s", astDefinitionTypeName(definition->type));
}
}

static void ppAstDefMacro(PrattUTF8 *dest, AstDefMacro *defMacro) {
psprintf(dest, "macro ");
ppHashSymbol(dest, defMacro->name);
psprintf(dest, "(");
ppAstArgList(dest, defMacro->definition->altArgs->argList);
psprintf(dest, ") ");
ppAstNest(dest, defMacro->definition->nest);
}

static void ppAstDefine(PrattUTF8 *dest, AstDefine *define) {
ppHashSymbol(dest, define->symbol);
psprintf(dest, " = ");
Expand Down
13 changes: 13 additions & 0 deletions src/lambda.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ structs:
args: LamVarList
exp: LamExp

LamMacro:
args: LamVarList
exp: LamExp
env: LamContext

LamVarList:
var: HashSymbol
next: LamVarList
Expand Down Expand Up @@ -142,6 +147,8 @@ structs:
LamContext:
frame: LamInfoTable
aliases: LamAliasTable
gensyms: LamGenSymTable
macros: LamMacroTable
parent: LamContext

LamAmb:
Expand Down Expand Up @@ -285,6 +292,12 @@ unions:
nsid: int

hashes:
LamMacroTable:
entries: LamMacro

LamGenSymTable:
entries: HashSymbol

LamInfoTable:
entries: LamInfo

Expand Down
Loading

0 comments on commit 74ef689

Please sign in to comment.