Skip to content

Commit

Permalink
feature: syntaxer partly support pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
park671 committed Oct 23, 2024
1 parent b79f384 commit 9bdded0
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 26 deletions.
45 changes: 43 additions & 2 deletions compiler/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef PCC_CC_AST_H
#define PCC_CC_AST_H

#include "stdint.h"

enum PrimitiveType {
TYPE_UNKNOWN,

Expand Down Expand Up @@ -72,17 +74,32 @@ enum ArithmeticFactorType {

enum ExpressionType {
EXPRESSION_ASSIGNMENT,
EXPRESSION_ARITHMETIC
EXPRESSION_ARITHMETIC,
EXPRESSION_POINTER,
};

struct AstType {
bool isPointer;
PrimitiveType primitiveType;
};

enum IdentityType {
ID_METHOD,
ID_VAR,
ID_ARRAY,
};

struct AstIdentity {
IdentityType type;
const char *name;
};

struct AstArray {
PrimitiveType primitiveType;
int size;
void *buffer;
};

struct AstParamDefine {
AstType *type;
AstIdentity *identity;
Expand Down Expand Up @@ -171,6 +188,28 @@ struct AstExpressionArithmetic {

struct AstExpression;

enum ExpressionPointerType {
EXP_POINTER_CALC,
EXP_POINTER_ARRAY,
};

struct AstExpressionPointerCalc {
//&
AstIdentity *identity;
};

struct AstExpressionPointerArray {
AstArray *array;
};

struct AstExpressionPointer {
ExpressionPointerType type;
union {
AstExpressionPointerCalc *pointerCalc;
AstExpressionPointerArray *pointerArray;
};
};

struct AstExpressionAssignment {
AstIdentity *identity;
//=
Expand All @@ -182,6 +221,7 @@ struct AstExpression {
union {
AstExpressionAssignment *assignmentExpression;
AstExpressionArithmetic *arithmeticExpression;
AstExpressionPointer *pointerExpression;
};
};

Expand All @@ -200,7 +240,7 @@ struct AstStatementDefine {
};

struct AstStatementMethodCall {
PrimitiveType retType;
AstType *retType;
AstIdentity *identity;
//(
AstObjectList *objectList;
Expand Down Expand Up @@ -320,6 +360,7 @@ enum AstNodeType {
NODE_STATEMENT_METHOD_CALL,
NODE_OBJECT_LIST,
NODE_EXPRESSION,
NODE_EXPRESSION_POINTER,
NODE_EXPRESSION_ASSIGNMENT,
NODE_EXPRESSION_ARITHMETIC,
NODE_EXPRESSION_ARITHMETIC_MORE,
Expand Down
57 changes: 42 additions & 15 deletions compiler/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ const char *getTokenTypeName(TokenType tokenType) {
return "bool";
case TOKEN_CHARS:
return "chars";
case TOKEN_POINTER:
case TOKEN_POINTER_TYPE:
return "pointer";
case TOKEN_POINTER_OPERATOR:
return "pointer_op";
}
return "unknown";
}
Expand Down Expand Up @@ -115,6 +117,10 @@ inline static bool isPointer(char a) {
return a == '*';
}

inline static bool isGetAddress(char a) {
return a == '&';
}

inline static char *makeCharsCopy(const char *origin) {
int length = strlen(origin) + 1;
char *copy = (char *) pccMalloc(VAR_TAG, sizeof(char) * length);
Expand Down Expand Up @@ -206,7 +212,18 @@ static Token *lexer(Token *tail, const char *buffer) {
continue;
} else if (isPointer(buffer[i])) {
if (tail->tokenType == TOKEN_TYPE) {
tail->tokenType = TOKEN_POINTER;
tail->tokenType = TOKEN_POINTER_TYPE;
continue;
} else {
tail = processCompletedString(tail, tmp);
Token *token = (Token *) (pccMalloc(LEXER_TAG, sizeof(Token)));
token->tokenType = TOKEN_POINTER_OPERATOR;
char *content = (char *) pccMalloc(VAR_TAG, sizeof(char) * 2);
content[0] = buffer[i];
content[1] = '\0';
token->content = content;
tail->next = token;
tail = token;
continue;
}
} else if (isBoundary(buffer[i])) {
Expand Down Expand Up @@ -244,20 +261,30 @@ static Token *lexer(Token *tail, const char *buffer) {
tail->next = token;
tail = token;
}
} else if (isBoolOperator(buffer[i])) {
} else if (isBoolOperator(buffer[i])
&& i + 1 < length
&& buffer[i + 1] == buffer[i]) {
tail = processCompletedString(tail, tmp);
if (i + 1 < length && buffer[i + 1] == buffer[i]) {
Token *token = (Token *) (pccMalloc(LEXER_TAG, sizeof(Token)));
token->tokenType = TOKEN_BOOL;
char *content = (char *) pccMalloc(VAR_TAG, sizeof(char) * 3);
content[0] = buffer[i];
content[1] = buffer[i + 1];
content[2] = '\0';
token->content = content;
tail->next = token;
tail = token;
i++;
}
Token *token = (Token *) (pccMalloc(LEXER_TAG, sizeof(Token)));
token->tokenType = TOKEN_BOOL;
char *content = (char *) pccMalloc(VAR_TAG, sizeof(char) * 3);
content[0] = buffer[i];
content[1] = buffer[i + 1];
content[2] = '\0';
token->content = content;
tail->next = token;
tail = token;
i++;
} else if (isGetAddress(buffer[i])) {
tail = processCompletedString(tail, tmp);
Token *token = (Token *) (pccMalloc(LEXER_TAG, sizeof(Token)));
token->tokenType = TOKEN_POINTER_OPERATOR;
char *content = (char *) pccMalloc(VAR_TAG, sizeof(char) * 2);
content[0] = buffer[i];
content[1] = '\0';
token->content = content;
tail->next = token;
tail = token;
} else {
snprintf(tmp, 256, "%s%c", tmp, buffer[i]);
}
Expand Down
10 changes: 7 additions & 3 deletions compiler/mir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@ static const char *convertBoolOpString(MirBooleanOperator mirBooleanOperator) {
}


MirOperandType convertAstType2MirType(PrimitiveType primitiveType) {
MirOperandType convertAstType2MirType(AstType *retType) {
if (retType->isPointer) {
return OPERAND_POINTER;
}
PrimitiveType primitiveType = retType->primitiveType;
switch (primitiveType) {
case TYPE_CHAR: {
return OPERAND_INT8;
Expand Down Expand Up @@ -1034,7 +1038,7 @@ void generateParam(AstParamList *astParamList, MirMethod *mirMethod) {
}
}
addVarInfo(mirMethodParam->paramName,
convertAstType2MirType(curAstParamList->paramDefine->type->primitiveType));
convertAstType2MirType(curAstParamList->paramDefine->type));

curAstParamList = curAstParamList->next;
}
Expand All @@ -1050,7 +1054,7 @@ void generateParam(AstParamList *astParamList, MirMethod *mirMethod) {
void generateMethod(AstMethodDefine *astMethodDefine, MirMethod *mirMethod) {
mirMethod->label = astMethodDefine->identity->name;
// logd(MIR_TAG, "--- mir method:%s", mirMethod->label);
addMethodInfo(astMethodDefine->identity->name, convertAstType2MirType(astMethodDefine->type->primitiveType));
addMethodInfo(astMethodDefine->identity->name, convertAstType2MirType(astMethodDefine->type));
if (astMethodDefine->paramList != nullptr) {
//var in method params need stack
generateParam(astMethodDefine->paramList, mirMethod);
Expand Down
4 changes: 3 additions & 1 deletion compiler/mir.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ enum MirOperandType {
OPERAND_INT64,

OPERAND_FLOAT32,
OPERAND_FLOAT64
OPERAND_FLOAT64,

OPERAND_POINTER
};

struct MirOperand {
Expand Down
25 changes: 21 additions & 4 deletions compiler/syntaxer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) {
case NODE_METHOD_DEFINE: {
AstMethodDefine *astMethodDefine = (AstMethodDefine *) currentNode;
//method type
if (token->tokenType != TOKEN_TYPE) {
if (token->tokenType != TOKEN_TYPE && token->tokenType != TOKEN_POINTER_TYPE) {
loge(SYNTAX_TAG, "[-]error: method define need type: %s", token->content);
return nullptr;
}
astMethodDefine->type = (AstType *) pccMalloc(SYNTAX_TAG, sizeof(AstType));
astMethodDefine->type->isPointer = (token->tokenType == TOKEN_POINTER_TYPE);
astMethodDefine->type->primitiveType = convertTokenType2PrimitiveType(token->content);
token = token->next;
//method name
Expand All @@ -122,6 +123,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) {
}
astMethodDefine->identity = (AstIdentity *) pccMalloc(SYNTAX_TAG, sizeof(AstIdentity));
astMethodDefine->identity->name = token->content;
astMethodDefine->identity->type = ID_METHOD;
token = token->next;
//method (
if (token->tokenType != TOKEN_BOUNDARY || strcmp(token->content, "(") != 0) {
Expand Down Expand Up @@ -165,19 +167,21 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) {
}
case NODE_PARAM_DEFINE: {
AstParamDefine *astParamDefine = (AstParamDefine *) currentNode;
if (token->tokenType != TOKEN_TYPE) {
if (token->tokenType != TOKEN_TYPE && token->tokenType != TOKEN_POINTER_TYPE) {
loge(SYNTAX_TAG, "[-]error: param define need type: %s", token->content);
return nullptr;
}
astParamDefine->type = (AstType *) pccMalloc(SYNTAX_TAG, sizeof(AstType));
astParamDefine->type->primitiveType = convertTokenType2PrimitiveType(token->content);
astParamDefine->type->isPointer = (token->tokenType == TOKEN_POINTER_TYPE);
token = token->next;
if (token->tokenType != TOKEN_IDENTIFIER) {
loge(SYNTAX_TAG, "[-]error: param define need identifier: %s", token->content);
return nullptr;
}
astParamDefine->identity = (AstIdentity *) pccMalloc(SYNTAX_TAG, sizeof(AstIdentity));
astParamDefine->identity->name = token->content;
astParamDefine->identity->type = ID_VAR;
addVar(astParamDefine->identity);
token = token->next;
break;
Expand Down Expand Up @@ -247,12 +251,13 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) {
token = token->next;
token = travelAst(token, astStatement->forStatement, NODE_STATEMENT_RETURN);
}
} else if (token->tokenType == TOKEN_TYPE) {
} else if (token->tokenType == TOKEN_TYPE || token->tokenType == TOKEN_POINTER_TYPE) {
astStatement->statementType = STATEMENT_DEFINE;
astStatement->defineStatement = (AstStatementDefine *) pccMalloc(SYNTAX_TAG,
sizeof(AstStatementDefine));
astStatement->defineStatement->type = (AstType *) pccMalloc(SYNTAX_TAG, sizeof(AstType));
astStatement->defineStatement->type->primitiveType = convertTokenType2PrimitiveType(token->content);
astStatement->defineStatement->type->isPointer = (token->tokenType == TOKEN_POINTER_TYPE);
token = token->next;
if (token->tokenType != TOKEN_IDENTIFIER) {
loge(SYNTAX_TAG, "[-]error: var define need identifier: %s", token->content);
Expand Down Expand Up @@ -338,6 +343,11 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) {
loge(SYNTAX_TAG, "[-]error: undefined var: %s", token->content);
}
break;
} else if(token->tokenType == TOKEN_POINTER_OPERATOR || token->tokenType == TOKEN_CHARS) {
astExpression->expressionType = EXPRESSION_POINTER;
astExpression->pointerExpression = (AstExpressionPointer *) pccMalloc(SYNTAX_TAG,
sizeof(AstExpressionPointer));
token = travelAst(token, astExpression->pointerExpression, NODE_EXPRESSION_POINTER);
} else {
astExpression->expressionType = EXPRESSION_ARITHMETIC;
astExpression->arithmeticExpression = (AstExpressionArithmetic *) pccMalloc(SYNTAX_TAG,
Expand All @@ -346,6 +356,10 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) {
}
break;
}
case NODE_EXPRESSION_POINTER: {

break;
}
case NODE_EXPRESSION_ASSIGNMENT: {
AstExpressionAssignment *astExpressionAssignment = (AstExpressionAssignment *) currentNode;
astExpressionAssignment->identity = (AstIdentity *) pccMalloc(SYNTAX_TAG, sizeof(AstIdentity));
Expand Down Expand Up @@ -641,6 +655,9 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) {
} else if (token->tokenType == TOKEN_BOUNDARY && strcmp(token->content, "(")) {
loge(SYNTAX_TAG, "[-]error: not impl yet: %s", token->content);
return nullptr;
} else if (token->tokenType == TOKEN_CHARS) {
loge(SYNTAX_TAG, "[-]error: not impl yet: %s", token->content);
return nullptr;
} else {
loge(SYNTAX_TAG, "[-]error: except valid identifier or num: %s", token->content);
return nullptr;
Expand All @@ -659,7 +676,7 @@ Token *travelAst(Token *token, void *currentNode, AstNodeType nodeType) {
astStatementMethodCall->identity->name);
exit(1);
}
astStatementMethodCall->retType = methodDefine->type->primitiveType;
astStatementMethodCall->retType = methodDefine->type;
//check next token
if (token->tokenType != TOKEN_BOUNDARY || strcmp(token->content, "(") != 0) {
loge(SYNTAX_TAG, "[-]error: method call need (: %s", token->content);
Expand Down
4 changes: 3 additions & 1 deletion compiler/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ enum TokenType {
TOKEN_BOUNDARY,
TOKEN_OPERATOR,
TOKEN_OPERATOR_2,
TOKEN_POINTER_OPERATOR,//* &
TOKEN_BOOL,
TOKEN_INTEGER,
TOKEN_FLOAT,
TOKEN_CHARS,
TOKEN_ARRAY,//todo: impl this
TOKEN_KEYWORD,
TOKEN_TYPE,
TOKEN_POINTER,
TOKEN_POINTER_TYPE,//field same to type, but means this type's pointer
TOKEN_IDENTIFIER
};

Expand Down

0 comments on commit 9bdded0

Please sign in to comment.