about summary refs log tree commit diff
path: root/src/bootstrap/parser.pry
diff options
context:
space:
mode:
authorBaitinq <[email protected]>2025-07-11 20:34:05 +0200
committerBaitinq <[email protected]>2025-07-11 20:34:05 +0200
commit22a46dc9b27fdd90e2dc307292b85bae42516e4f (patch)
treeebfaba570113926c694702719fb85c6fd86b83ab /src/bootstrap/parser.pry
parentBoostrap: Parse ( expr ) (diff)
downloadpry-lang-22a46dc9b27fdd90e2dc307292b85bae42516e4f.tar.gz
pry-lang-22a46dc9b27fdd90e2dc307292b85bae42516e4f.tar.bz2
pry-lang-22a46dc9b27fdd90e2dc307292b85bae42516e4f.zip
Boostrap: Support cast statement
Diffstat (limited to '')
-rw-r--r--src/bootstrap/parser.pry56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/bootstrap/parser.pry b/src/bootstrap/parser.pry
index b24ec33..29aba73 100644
--- a/src/bootstrap/parser.pry
+++ b/src/bootstrap/parser.pry
@@ -278,6 +278,50 @@ let parser_parse_return_statement = (p: *parser) => *Node {
 
 extern parser_parse_type = (*parser) => *Node;
 
+/* CastStatement ::= "cast" LPAREN TYPE "," Expression RPAREN */
+let parser_parse_cast_statement = (p: *parser) => *Node {
+	let ident = parser_accept_token(p, TOKEN_IDENTIFIER);
+	if ident == cast(*token, null) {
+		return cast(*Node, null);
+	};
+
+	if !strcmp(cast(*i8, (*ident).data), "cast") {
+		return cast(*Node, null);
+	};
+
+	if parser_accept_token(p, TOKEN_LPAREN) == cast(*token, null) {
+		return cast(*Node, null);
+	};
+	
+	let typ = parser_parse_type(p);
+	if typ == cast(*Node, null) {
+		return cast(*Node, null);
+	};
+	
+	if parser_accept_token(p, TOKEN_COMMA) == cast(*token, null) {
+		return cast(*Node, null);
+	};
+
+	let expression = parser_parse_expression(p);
+	if expression == cast(*Node, null) {
+	    return cast(*Node, null);
+	};
+	
+	if parser_accept_token(p, TOKEN_RPAREN) == cast(*token, null) {
+		return cast(*Node, null);
+	};
+
+	let d = cast(*NODE_CAST_STATEMENT_DATA , arena_alloc((*p).arena, sizeof(NODE_CAST_STATEMENT_DATA )));
+	(*d).typ = typ;
+	(*d).expression = expression;
+
+	let r = Node{};
+	r.type = NODE_CAST_STATEMENT;
+	r.data = cast(*void, d);
+
+	return create_node(p, r);
+};
+
 /* FunctionType ::= LPAREN (Type ("," Type)*)? RPAREN ARROW Type */
 let parser_parse_function_type = (p: *parser) => *Node {
 	if parser_accept_token(p, TOKEN_LPAREN) == cast(*token, null) {
@@ -756,6 +800,10 @@ let parser_parse_equality_expression = (p: *parser) => *Node {
 
 /* PostfixExpression ::= PrimaryExpression (CastStatement | SizeOfStatement | FunctionCallStatement | FieldAccess )* */
 let parser_parse_postfix_expression = (p: *parser) => *Node {
+	let ex = parser_accept_parse(p, parser_parse_cast_statement);
+	if ex != cast(*Node, null) {
+		return ex;
+	};
 	let ex = parser_accept_parse(p, parse_function_call_statement);
 	if ex != cast(*Node, null) {
 		return ex;
@@ -973,6 +1021,14 @@ let parse_function_call_statement = (p: *parser) => *Node {
 
 /* Statement    ::= (AssignmentStatement | ImportDeclaration | ExternDeclaration | CastStatement | SizeOfStatement | FunctionCallStatement | IfStatement | WhileStatement | ReturnStatement | "break" | "continue") SEMICOLON */
 let parser_parse_statement = (p: *parser) => *Node {
+	let fn_call = parser_accept_parse(p, parser_parse_cast_statement);
+	if fn_call != cast(*Node, null) {
+		if parser_accept_token(p, TOKEN_SEMICOLON) == cast(*token, null) {
+		    return cast(*Node, null);
+		};
+	    return fn_call;
+	};
+
 	let fn_call = parser_accept_parse(p, parse_function_call_statement);
 	if fn_call != cast(*Node, null) {
 		if parser_accept_token(p, TOKEN_SEMICOLON) == cast(*token, null) {