Skip to content

Commit e710fdf

Browse files
committed
implemented Pillar 10: Zero-Trust Security.
1 parent 540d2d8 commit e710fdf

3 files changed

Lines changed: 62 additions & 7 deletions

File tree

src/compiler/parser/ast.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,16 @@ Expr *createSanitizeExpr(Expr *value, int line, int column) {
359359
return expr;
360360
}
361361

362+
Expr *createCryptoExpr(Expr *val, bool isEncrypt, int line, int column) {
363+
Expr *expr = ALLOCATE(Expr, 1);
364+
expr->type = EXPR_CRYPTO;
365+
expr->line = line;
366+
expr->column = column;
367+
expr->as.crypto.value = val;
368+
expr->as.crypto.isEncrypt = isEncrypt;
369+
return expr;
370+
}
371+
362372
// --- Statement Creation Functions ---
363373

364374
Stmt *createExpressionStmt(Expr *expression, int line, int column) {
@@ -645,6 +655,16 @@ Stmt *createGPUBlockStmt(const char *kernelName, StmtList *body, int line, int c
645655
return stmt;
646656
}
647657

658+
Stmt *createVerifyStmt(const char *identityName, StmtList *body, int line, int column) {
659+
Stmt *stmt = ALLOCATE(Stmt, 1);
660+
stmt->type = STMT_VERIFY;
661+
stmt->line = line;
662+
stmt->column = column;
663+
stmt->as.verify_stmt.identityName = strdup(identityName);
664+
stmt->as.verify_stmt.body = body;
665+
return stmt;
666+
}
667+
648668
// --- Free Functions ---
649669

650670
void freeExpr(Expr *expr) {
@@ -732,6 +752,9 @@ void freeExpr(Expr *expr) {
732752
case EXPR_SANITIZE:
733753
freeExpr(expr->as.sanitize.value);
734754
break;
755+
case EXPR_CRYPTO:
756+
freeExpr(expr->as.crypto.value);
757+
break;
735758
}
736759

737760
FREE(Expr, expr);
@@ -859,6 +882,10 @@ void freeStmt(Stmt *stmt) {
859882
if(stmt->as.gpu_block.kernelName) free(stmt->as.gpu_block.kernelName);
860883
if(stmt->as.gpu_block.body) freeStmtList(stmt->as.gpu_block.body);
861884
break;
885+
case STMT_VERIFY:
886+
free(stmt->as.verify_stmt.identityName);
887+
if(stmt->as.verify_stmt.body) freeStmtList(stmt->as.verify_stmt.body);
888+
break;
862889
}
863890

864891
FREE(Stmt, stmt);

src/compiler/parser/parser.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ static Stmt *distributedDecl(Parser *p); // Forward
2828
static Stmt *modelDecl(Parser *p); // Forward
2929
static Stmt *quantumStmt(Parser *p); // Forward
3030
static Stmt *gpuStmt(Parser *p); // Forward
31+
static Stmt *verifyStmt(Parser *p); // Forward
3132
static Stmt *useDecl(Parser *p);
3233
static Stmt *forStmt(Parser *p);
3334
static Stmt *ifStmt(Parser *p);
@@ -276,6 +277,8 @@ static Stmt *declaration(Parser *p) {
276277
return quantumStmt(p);
277278
if (match(p, 1, TOKEN_GPU))
278279
return gpuStmt(p);
280+
if (match(p, 1, TOKEN_VERIFY))
281+
return verifyStmt(p);
279282

280283
return statement(p);
281284
}
@@ -1013,17 +1016,30 @@ static Expr *call(Parser *p) {
10131016
}
10141017
}
10151018

1016-
return expr;
1017-
}
1018-
10191019
static Expr *primary(Parser *p) {
10201020
if (match(p, 1, TOKEN_SANITIZE)) {
1021-
consume(p, TOKEN_LEFT_PAREN, "Expect '(' after sanitize.");
1021+
consume(p, TOKEN_LEFT_PAREN, "Expect '(' after sanitize.");
1022+
Expr *val = expression(p);
1023+
consume(p, TOKEN_RIGHT_PAREN, "Expect ')' after sanitize expression.");
1024+
return createSanitizeExpr(val, p->previous.line, 0);
1025+
}
1026+
1027+
if (match(p, 1, TOKEN_ENCRYPT)) {
1028+
consume(p, TOKEN_LEFT_PAREN, "Expect '(' after encrypt.");
10221029
Expr *val = expression(p);
1023-
consume(p, TOKEN_RIGHT_PAREN, "Expect ')'.");
1024-
return createSanitizeExpr(val, p->previous.line, 0);
1030+
consume(p, TOKEN_RIGHT_PAREN, "Expect ')' after encrypt expression.");
1031+
// Optional 'using <key>' parsing could be added here
1032+
return createCryptoExpr(val, true, p->previous.line, 0);
10251033
}
1026-
if (match(p, 1, TOKEN_FALSE)) return createLiteralExpr((Value){VAL_BOOL, {.boolean = false}}, p->previous.line, 0);
1034+
1035+
if (match(p, 1, TOKEN_DECRYPT)) {
1036+
consume(p, TOKEN_LEFT_PAREN, "Expect '(' after decrypt.");
1037+
Expr *val = expression(p);
1038+
consume(p, TOKEN_RIGHT_PAREN, "Expect ')' after decrypt expression.");
1039+
return createCryptoExpr(val, false, p->previous.line, 0);
1040+
}
1041+
1042+
if (match(p, 1, TOKEN_FALSE)) return createLiteralExpr(LITERAL_BOOL, "false", p->previous.line, 0);
10271043
if (match(p, 1, TOKEN_TRUE)) return createLiteralExpr((Value){VAL_BOOL, {.boolean = true}}, p->previous.line, 0);
10281044
if (match(p, 1, TOKEN_NULL)) {
10291045
return createLiteralExpr(NULL_VAL, p->previous.line, 0);

src/compiler/type_checker.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,18 @@ static void checkStmt(TypeChecker* checker, Stmt* stmt) {
624624
break;
625625
}
626626

627+
case STMT_VERIFY: {
628+
beginScope(checker);
629+
StmtList* body = stmt->as.verify_stmt.body;
630+
if (body) {
631+
for(int i=0; i<body->count; i++) {
632+
checkStmt(checker, body->items[i]);
633+
}
634+
}
635+
endScope(checker);
636+
break;
637+
}
638+
627639
default:
628640
break;
629641
}

0 commit comments

Comments
 (0)