forked from cosmonaut/wraith-lang
				
			add custom types and local allocation
							parent
							
								
									48d049b6c9
								
							
						
					
					
						commit
						e3fd821d8f
					
				
							
								
								
									
										13
									
								
								ast.c
								
								
								
								
							
							
						
						
									
										13
									
								
								ast.c
								
								
								
								
							|  | @ -53,6 +53,18 @@ Node* MakeTypeNode( | ||||||
|     return node; |     return node; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | Node* MakeCustomTypeNode( | ||||||
|  |     Node *identifierNode | ||||||
|  | ) { | ||||||
|  |     Node* node = (Node*) malloc(sizeof(Node)); | ||||||
|  |     node->syntaxKind = Type; | ||||||
|  |     node->type = CustomType; | ||||||
|  |     node->childCount = 1; | ||||||
|  |     node->children = (Node**) malloc(sizeof(Node*)); | ||||||
|  |     node->children[0] = identifierNode; | ||||||
|  |     return node; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| Node* MakeIdentifierNode( | Node* MakeIdentifierNode( | ||||||
|     const char *id |     const char *id | ||||||
| ) { | ) { | ||||||
|  | @ -288,6 +300,7 @@ static const char* PrimitiveTypeToString(PrimitiveType type) | ||||||
|         case UInt: return "UInt"; |         case UInt: return "UInt"; | ||||||
|         case Bool: return "Bool"; |         case Bool: return "Bool"; | ||||||
|         case Void: return "Void"; |         case Void: return "Void"; | ||||||
|  |         case CustomType: return "CustomType"; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     return "Unknown"; |     return "Unknown"; | ||||||
|  |  | ||||||
							
								
								
									
										6
									
								
								ast.h
								
								
								
								
							
							
						
						
									
										6
									
								
								ast.h
								
								
								
								
							|  | @ -48,7 +48,8 @@ typedef enum | ||||||
|     UInt, |     UInt, | ||||||
|     Float, |     Float, | ||||||
|     Double, |     Double, | ||||||
|     String |     String, | ||||||
|  |     CustomType | ||||||
| } PrimitiveType; | } PrimitiveType; | ||||||
| 
 | 
 | ||||||
| typedef union | typedef union | ||||||
|  | @ -81,6 +82,9 @@ const char* SyntaxKindString(SyntaxKind syntaxKind); | ||||||
| Node* MakeTypeNode( | Node* MakeTypeNode( | ||||||
|     PrimitiveType type |     PrimitiveType type | ||||||
| ); | ); | ||||||
|  | Node* MakeCustomTypeNode( | ||||||
|  |     Node *identifierNode | ||||||
|  | ); | ||||||
| Node* MakeIdentifierNode( | Node* MakeIdentifierNode( | ||||||
|     const char *id |     const char *id | ||||||
| ); | ); | ||||||
|  |  | ||||||
							
								
								
									
										58
									
								
								compiler.c
								
								
								
								
							
							
						
						
									
										58
									
								
								compiler.c
								
								
								
								
							|  | @ -228,6 +228,38 @@ static LLVMValueRef FindVariableByName(LLVMBuilderRef builder, LLVMValueRef wStr | ||||||
|     return searchResult; |     return searchResult; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | typedef struct CustomTypeMap | ||||||
|  | { | ||||||
|  |     LLVMTypeRef type; | ||||||
|  |     char *name; | ||||||
|  | } CustomTypeMap; | ||||||
|  | 
 | ||||||
|  | CustomTypeMap *customTypes; | ||||||
|  | uint32_t customTypeCount; | ||||||
|  | 
 | ||||||
|  | static void RegisterCustomType(LLVMTypeRef type, char *name) | ||||||
|  | { | ||||||
|  |     customTypes = realloc(customTypes, sizeof(CustomType) * (customTypeCount + 1)); | ||||||
|  |     customTypes[customTypeCount].type = type; | ||||||
|  |     customTypes[customTypeCount].name = strdup(name); | ||||||
|  |     customTypeCount += 1; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | static LLVMTypeRef LookupCustomType(char *name) | ||||||
|  | { | ||||||
|  |     uint32_t i; | ||||||
|  | 
 | ||||||
|  |     for (i = 0; i < customTypeCount; i += 1) | ||||||
|  |     { | ||||||
|  |         if (strcmp(customTypes[i].name, name) == 0) | ||||||
|  |         { | ||||||
|  |             return customTypes[i].type; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return NULL; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| static LLVMTypeRef WraithTypeToLLVMType(PrimitiveType type) | static LLVMTypeRef WraithTypeToLLVMType(PrimitiveType type) | ||||||
| { | { | ||||||
|     switch (type) |     switch (type) | ||||||
|  | @ -344,6 +376,23 @@ static void CompileAssignment(LLVMValueRef wStructValue, LLVMBuilderRef builder, | ||||||
|     MarkStructFieldForWrite(wStructValue, identifier); |     MarkStructFieldForWrite(wStructValue, identifier); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | static void CompileFunctionVariableDeclaration(LLVMBuilderRef builder, Node *variableDeclaration) | ||||||
|  | { | ||||||
|  |     char *variableName = variableDeclaration->children[1]->value.string; | ||||||
|  |     LLVMValueRef variable; | ||||||
|  |     if (variableDeclaration->children[0]->type == CustomType) | ||||||
|  |     { | ||||||
|  |         char *customTypeName = variableDeclaration->children[0]->children[0]->value.string; | ||||||
|  |         variable = LLVMBuildAlloca(builder, LookupCustomType(customTypeName), variableName); | ||||||
|  |     } | ||||||
|  |     else | ||||||
|  |     { | ||||||
|  |         variable = LLVMBuildAlloca(builder, WraithTypeToLLVMType(variableDeclaration->children[0]->type), variableName); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     AddNamedVariable(variableName, variable); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| static uint8_t CompileStatement(LLVMValueRef wStructValue, LLVMBuilderRef builder, LLVMValueRef function, Node *statement) | static uint8_t CompileStatement(LLVMValueRef wStructValue, LLVMBuilderRef builder, LLVMValueRef function, Node *statement) | ||||||
| { | { | ||||||
|     switch (statement->syntaxKind) |     switch (statement->syntaxKind) | ||||||
|  | @ -352,6 +401,10 @@ static uint8_t CompileStatement(LLVMValueRef wStructValue, LLVMBuilderRef builde | ||||||
|             CompileAssignment(wStructValue, builder, function, statement); |             CompileAssignment(wStructValue, builder, function, statement); | ||||||
|             return 0; |             return 0; | ||||||
| 
 | 
 | ||||||
|  |         case Declaration: | ||||||
|  |             CompileFunctionVariableDeclaration(builder, statement); | ||||||
|  |             return 0; | ||||||
|  | 
 | ||||||
|         case Return: |         case Return: | ||||||
|             CompileReturn(wStructValue, builder, function, statement); |             CompileReturn(wStructValue, builder, function, statement); | ||||||
|             return 1; |             return 1; | ||||||
|  | @ -470,6 +523,8 @@ static void CompileStruct(LLVMModuleRef module, LLVMContextRef context, Node *no | ||||||
|                 break; |                 break; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     RegisterCustomType(wStruct, node->children[0]->value.string); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static void Compile(LLVMModuleRef module, LLVMContextRef context, Node *node) | static void Compile(LLVMModuleRef module, LLVMContextRef context, Node *node) | ||||||
|  | @ -503,6 +558,9 @@ int main(int argc, char *argv[]) | ||||||
|     structFieldMaps = NULL; |     structFieldMaps = NULL; | ||||||
|     structFieldMapCount = 0; |     structFieldMapCount = 0; | ||||||
| 
 | 
 | ||||||
|  |     customTypes = NULL; | ||||||
|  |     customTypeCount = 0; | ||||||
|  | 
 | ||||||
|     stack = CreateStack(); |     stack = CreateStack(); | ||||||
| 
 | 
 | ||||||
|     FILE *fp = fopen(argv[1], "r"); |     FILE *fp = fopen(argv[1], "r"); | ||||||
|  |  | ||||||
							
								
								
									
										12
									
								
								stack.c
								
								
								
								
							
							
						
						
									
										12
									
								
								stack.c
								
								
								
								
							|  | @ -4,7 +4,7 @@ | ||||||
| 
 | 
 | ||||||
| Stack* CreateStack() | Stack* CreateStack() | ||||||
| { | { | ||||||
|     uint32_t i; |     int32_t i; | ||||||
|     Stack *stack = (Stack*) malloc(sizeof(Stack)); |     Stack *stack = (Stack*) malloc(sizeof(Stack)); | ||||||
|     stack->stackCapacity = 4; |     stack->stackCapacity = 4; | ||||||
|     stack->stackFrames = (StackFrame*) malloc(sizeof(StackFrame) * stack->stackCapacity); |     stack->stackFrames = (StackFrame*) malloc(sizeof(StackFrame) * stack->stackCapacity); | ||||||
|  | @ -15,6 +15,8 @@ Stack* CreateStack() | ||||||
|         stack->stackFrames[i].nodeCount = 0; |         stack->stackFrames[i].nodeCount = 0; | ||||||
|     } |     } | ||||||
|     stack->stackIndex = 0; |     stack->stackIndex = 0; | ||||||
|  | 
 | ||||||
|  |     PushStackFrame(stack); | ||||||
|     return stack; |     return stack; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -26,8 +28,9 @@ void PushStackFrame(Stack *stack) | ||||||
|     { |     { | ||||||
|         stack->stackCapacity += 1; |         stack->stackCapacity += 1; | ||||||
|         stack->stackFrames = (StackFrame*) realloc(stack->stackFrames, sizeof(StackFrame) * stack->stackCapacity); |         stack->stackFrames = (StackFrame*) realloc(stack->stackFrames, sizeof(StackFrame) * stack->stackCapacity); | ||||||
| 
 |         stack->stackFrames[stack->stackIndex].nodes = NULL; | ||||||
|         stack->stackFrames[stack->stackIndex].nodeCapacity = 0; |         stack->stackFrames[stack->stackIndex].nodeCapacity = 0; | ||||||
|  |         stack->stackFrames[stack->stackIndex].nodeCount = 0; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     stack->stackFrames[stack->stackIndex].nodeCount = 0; |     stack->stackFrames[stack->stackIndex].nodeCount = 0; | ||||||
|  | @ -52,6 +55,11 @@ void AddNode(Stack *stack, Node *statementNode) | ||||||
| 
 | 
 | ||||||
| Node** GetNodes(Stack *stack, uint32_t *pCount) | Node** GetNodes(Stack *stack, uint32_t *pCount) | ||||||
| { | { | ||||||
|  |     if (stack->stackIndex < 0) { | ||||||
|  |         *pCount= 0; | ||||||
|  |         return NULL; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     *pCount = stack->stackFrames[stack->stackIndex].nodeCount; |     *pCount = stack->stackFrames[stack->stackIndex].nodeCount; | ||||||
|     return stack->stackFrames[stack->stackIndex].nodes; |     return stack->stackFrames[stack->stackIndex].nodes; | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										4
									
								
								stack.h
								
								
								
								
							
							
						
						
									
										4
									
								
								stack.h
								
								
								
								
							|  | @ -13,8 +13,8 @@ typedef struct StackFrame | ||||||
| typedef struct Stack | typedef struct Stack | ||||||
| { | { | ||||||
|     StackFrame *stackFrames; |     StackFrame *stackFrames; | ||||||
|     uint32_t stackCapacity; |     int32_t stackCapacity; | ||||||
|     uint32_t stackIndex; |     int32_t stackIndex; | ||||||
| } Stack; | } Stack; | ||||||
| 
 | 
 | ||||||
| Stack* CreateStack(); | Stack* CreateStack(); | ||||||
|  |  | ||||||
							
								
								
									
										4
									
								
								wraith.y
								
								
								
								
							
							
						
						
									
										4
									
								
								wraith.y
								
								
								
								
							|  | @ -158,6 +158,10 @@ VariableDeclaration     : Type Identifier | ||||||
|                         { |                         { | ||||||
|                             $$ = MakeDeclarationNode($1, $2); |                             $$ = MakeDeclarationNode($1, $2); | ||||||
|                         } |                         } | ||||||
|  |                         | Identifier Identifier | ||||||
|  |                         { | ||||||
|  |                             $$ = MakeDeclarationNode(MakeCustomTypeNode($1), $2); | ||||||
|  |                         } | ||||||
| 
 | 
 | ||||||
| AssignmentStatement     : VariableDeclaration EQUAL Expression | AssignmentStatement     : VariableDeclaration EQUAL Expression | ||||||
|                         { |                         { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue