Skip to content

Commit

Permalink
Added indexing to strings, tests still needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratstail91 committed Nov 9, 2024
1 parent 5588986 commit 1925d41
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 42 deletions.
11 changes: 11 additions & 0 deletions source/toy_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ void Toy_private_emitAstGroup(Toy_Bucket** bucketHandle, Toy_Ast** astHandle) {
(*astHandle) = tmp;
}

void Toy_private_emitAstCompound(Toy_Bucket** bucketHandle, Toy_Ast** astHandle, Toy_AstFlag flag, Toy_Ast* right) {
Toy_Ast* tmp = (Toy_Ast*)Toy_partitionBucket(bucketHandle, sizeof(Toy_Ast));

tmp->type = TOY_AST_COMPOUND;
tmp->compound.flag = flag;
tmp->compound.left = *astHandle; //left-recursive
tmp->compound.right = right;

(*astHandle) = tmp;
}

void Toy_private_emitAstPrint(Toy_Bucket** bucketHandle, Toy_Ast** astHandle) {
Toy_Ast* tmp = (Toy_Ast*)Toy_partitionBucket(bucketHandle, sizeof(Toy_Ast));

Expand Down
25 changes: 19 additions & 6 deletions source/toy_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ typedef enum Toy_AstType {
TOY_AST_BINARY,
TOY_AST_COMPARE,
TOY_AST_GROUP,
TOY_AST_COMPOUND,

TOY_AST_PRINT,

Expand Down Expand Up @@ -52,14 +53,17 @@ typedef enum Toy_AstFlag {
TOY_AST_FLAG_COMPARE_GREATER = 24,
TOY_AST_FLAG_COMPARE_GREATER_EQUAL = 25,

TOY_AST_FLAG_AND = 30,
TOY_AST_FLAG_OR = 31,
TOY_AST_FLAG_CONCAT = 32,
TOY_AST_FLAG_COMPOUND_COLLECTION = 30,
TOY_AST_FLAG_COMPOUND_INDEX = 31,

TOY_AST_FLAG_AND = 40,
TOY_AST_FLAG_OR = 41,
TOY_AST_FLAG_CONCAT = 42,

//unary flags
TOY_AST_FLAG_NEGATE = 33,
TOY_AST_FLAG_INCREMENT = 34,
TOY_AST_FLAG_DECREMENT = 35,
TOY_AST_FLAG_NEGATE = 43,
TOY_AST_FLAG_INCREMENT = 44,
TOY_AST_FLAG_DECREMENT = 45,

// TOY_AST_FLAG_TERNARY,
} Toy_AstFlag;
Expand Down Expand Up @@ -105,6 +109,13 @@ typedef struct Toy_AstGroup {
Toy_Ast* child;
} Toy_AstGroup;

typedef struct Toy_AstCompound {
Toy_AstType type;
Toy_AstFlag flag;
Toy_Ast* left;
Toy_Ast* right;
} Toy_AstCompound;

typedef struct Toy_AstPrint {
Toy_AstType type;
Toy_Ast* child;
Expand Down Expand Up @@ -148,6 +159,7 @@ union Toy_Ast { //32 | 64 BITNESS
Toy_AstBinary binary; //16 | 24
Toy_AstCompare compare; //16 | 24
Toy_AstGroup group; //8 | 16
Toy_AstCompound compound; //16 | 24
Toy_AstPrint print; //8 | 16
Toy_AstVarDeclare varDeclare; //16 | 24
Toy_AstVarAssign varAssign; //16 | 24
Expand All @@ -165,6 +177,7 @@ void Toy_private_emitAstUnary(Toy_Bucket** bucketHandle, Toy_Ast** astHandle, To
void Toy_private_emitAstBinary(Toy_Bucket** bucketHandle, Toy_Ast** astHandle,Toy_AstFlag flag, Toy_Ast* right);
void Toy_private_emitAstCompare(Toy_Bucket** bucketHandle, Toy_Ast** astHandle,Toy_AstFlag flag, Toy_Ast* right);
void Toy_private_emitAstGroup(Toy_Bucket** bucketHandle, Toy_Ast** astHandle);
void Toy_private_emitAstCompound(Toy_Bucket** bucketHandle, Toy_Ast** astHandle,Toy_AstFlag flag, Toy_Ast* right);

void Toy_private_emitAstPrint(Toy_Bucket** bucketHandle, Toy_Ast** astHandle);

Expand Down
1 change: 1 addition & 0 deletions source/toy_opcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ typedef enum Toy_OpcodeType {
//various action instructions
TOY_OPCODE_PRINT,
TOY_OPCODE_CONCAT,
TOY_OPCODE_INDEX,
//TODO: clear the program stack?

//meta instructions
Expand Down
29 changes: 27 additions & 2 deletions source/toy_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ static Toy_AstFlag literal(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_As
static Toy_AstFlag unary(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast** rootHandle);
static Toy_AstFlag binary(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast** rootHandle);
static Toy_AstFlag group(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast** rootHandle);
static Toy_AstFlag compound(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast** rootHandle);

//precedence definitions
static ParsingTuple parsingRulesetTable[] = {
Expand Down Expand Up @@ -194,7 +195,7 @@ static ParsingTuple parsingRulesetTable[] = {
//structural operators
{PREC_NONE,group,NULL},// TOY_TOKEN_OPERATOR_PAREN_LEFT,
{PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_PAREN_RIGHT,
{PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_BRACKET_LEFT,
{PREC_CALL,NULL,compound},// TOY_TOKEN_OPERATOR_BRACKET_LEFT,
{PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_BRACKET_RIGHT,
{PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_BRACE_LEFT,
{PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_BRACE_RIGHT,
Expand All @@ -207,7 +208,7 @@ static ParsingTuple parsingRulesetTable[] = {
{PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_COLON,

{PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_SEMICOLON, // ;
{PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_COMMA, // ,
{PREC_CALL,NULL,compound},// TOY_TOKEN_OPERATOR_COMMA, // ,

{PREC_NONE,NULL,NULL},// TOY_TOKEN_OPERATOR_DOT, // .
{PREC_CALL,NULL,binary},// TOY_TOKEN_OPERATOR_CONCAT, // ..
Expand Down Expand Up @@ -557,6 +558,26 @@ static Toy_AstFlag group(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast*
return TOY_AST_FLAG_NONE;
}

static Toy_AstFlag compound(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast** rootHandle) {
//infix must advance
advance(parser);

if (parser->previous.type == TOY_TOKEN_OPERATOR_COMMA) {
parsePrecedence(bucketHandle, parser, rootHandle, PREC_ASSIGNMENT + 1);
return TOY_AST_FLAG_COMPOUND_COLLECTION;
}
else if (parser->previous.type == TOY_TOKEN_OPERATOR_BRACKET_LEFT) {
parsePrecedence(bucketHandle, parser, rootHandle, PREC_ASSIGNMENT + 1);
consume(parser, TOY_TOKEN_OPERATOR_BRACKET_RIGHT, "Expected ']' at the end of index expression");
return TOY_AST_FLAG_COMPOUND_INDEX;
}
else {
printError(parser, parser->previous, "Unexpected token passed to compound precedence rule");
Toy_private_emitAstError(bucketHandle, rootHandle);
return TOY_AST_FLAG_NONE;
}
}

static ParsingTuple* getParsingRule(Toy_TokenType type) {
return &parsingRulesetTable[type];
}
Expand Down Expand Up @@ -605,13 +626,17 @@ static void parsePrecedence(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_A
(*rootHandle) = ptr;
return;
}
//eww, gross
else if (flag >= 10 && flag <= 19) {
Toy_String* name = Toy_createNameStringLength(bucketHandle, prevToken.lexeme, prevToken.length, TOY_VALUE_UNKNOWN, false);
Toy_private_emitAstVariableAssignment(bucketHandle, rootHandle, name, flag, ptr);
}
else if (flag >= 20 && flag <= 29) {
Toy_private_emitAstCompare(bucketHandle, rootHandle, flag, ptr);
}
else if (flag >= 30 && flag <= 39) {
Toy_private_emitAstCompound(bucketHandle, rootHandle, flag, ptr);
}
else {
Toy_private_emitAstBinary(bucketHandle, rootHandle, flag, ptr);
}
Expand Down
Loading

0 comments on commit 1925d41

Please sign in to comment.