File splitting, object type

This commit is contained in:
VegOwOtenks 2024-10-10 15:54:22 +02:00
parent 5313cc5603
commit 3f05fa5141
14 changed files with 194 additions and 15 deletions

View file

@ -72,6 +72,8 @@ int BuiltinFunction_Plus(CallFrame* top_frame)
case VALUETYPE_BOOLEAN: case VALUETYPE_BOOLEAN:
result.get.boolean = v2.get.boolean + v1.get.boolean; result.get.boolean = v2.get.boolean + v1.get.boolean;
break; break;
case VALUETYPE_OBJECT:
return EXIT_FAILURE;
} }
top_frame->instruction_pointer++; top_frame->instruction_pointer++;
@ -105,6 +107,8 @@ int BuiltinFunction_Multiply(CallFrame* top_frame)
case VALUETYPE_BOOLEAN: case VALUETYPE_BOOLEAN:
result.get.boolean = v2.get.boolean * v1.get.boolean; result.get.boolean = v2.get.boolean * v1.get.boolean;
break; break;
case VALUETYPE_OBJECT:
return EXIT_FAILURE;
} }
top_frame->instruction_pointer++; top_frame->instruction_pointer++;
@ -138,6 +142,8 @@ int BuiltinFunction_Minus(CallFrame* top_frame)
case VALUETYPE_BOOLEAN: case VALUETYPE_BOOLEAN:
result.get.boolean = v2.get.boolean - v1.get.boolean; result.get.boolean = v2.get.boolean - v1.get.boolean;
break; break;
case VALUETYPE_OBJECT:
return EXIT_FAILURE;
} }
top_frame->instruction_pointer++; top_frame->instruction_pointer++;
@ -167,6 +173,17 @@ int BuiltinFunction_PrintLine(CallFrame* top_frame)
printf("false\n"); printf("false\n");
} }
break; break;
case VALUETYPE_OBJECT:
if (v1.get.object != NULL) {
StringView type_name_view = v1.get.object->type->name;
char type_name[type_name_view.length];
type_name[type_name_view.length] = '\0';
StringView_Paste(type_name, type_name_view);
printf("object (%s) at %p\n", type_name, (void*) v1.get.object);
break;
} else {
puts("null");
}
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;

View file

@ -23,9 +23,9 @@
#include "function.h" #include "function.h"
#include "variable.h" #include "variable.h"
#include "value.h" #include "value.h"
#include "dynamicarray/dynamicarray.h" #include "../submodules/utilitiec/src/dynamicarray/dynamicarray.h"
#include "StringView/StringView.h" #include "../submodules/utilitiec/src/StringView/StringView.h"
#include "Scratchpad/Scratchpad.h" #include "../submodules/utilitiec/src/Scratchpad/Scratchpad.h"
typedef struct CallFrame_s { typedef struct CallFrame_s {
size_t instruction_pointer; size_t instruction_pointer;

View file

@ -21,7 +21,7 @@
#define FLUP_FUNCTION_H #define FLUP_FUNCTION_H
#include <stdlib.h> #include <stdlib.h>
#include "StringView/StringView.h" #include "../submodules/utilitiec/src/StringView/StringView.h"
typedef struct FlupFunctionAlternative_s { typedef struct FlupFunctionAlternative_s {
size_t condition_token_start; size_t condition_token_start;

View file

@ -478,6 +478,10 @@ int Interpreter_ExecuteNext(Interpreter* self, size_t stop_token)
break; break;
} }
case TOKENTYPE_COMMENT:
top_frame->instruction_pointer++;
break;
case TOKENTYPE_PIPE: case TOKENTYPE_PIPE:
case TOKENTYPE_SEMICOLON: case TOKENTYPE_SEMICOLON:
case TOKENTYPE_LEFT_BRACE: case TOKENTYPE_LEFT_BRACE:
@ -494,6 +498,7 @@ int Interpreter_ExecuteNext(Interpreter* self, size_t stop_token)
case TOKENTYPE_COMMA: case TOKENTYPE_COMMA:
case TOKENTYPE_LEFT_PAREN: case TOKENTYPE_LEFT_PAREN:
case TOKENTYPE_RIGHT_PAREN: case TOKENTYPE_RIGHT_PAREN:
case TOKENTYPE_STRING:
fprintf(stderr, "Unexpected token with type: %s, continuing with next token...\n", TokenType_ToString(t->type)); fprintf(stderr, "Unexpected token with type: %s, continuing with next token...\n", TokenType_ToString(t->type));
top_frame->instruction_pointer++; top_frame->instruction_pointer++;
break; break;

41
src/object-type.h Normal file
View file

@ -0,0 +1,41 @@
/*
* This code is part of the programming language flup
* flup comes with ABSOLUTELY NO WARRANTY and is licensed under AGPL-3.0 or later.
* Copyright (C) 2024 VegOwOtenks
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef FLUP_OBJECTTYPE_H
#define FLUP_OBJECTTYPE_H
#include "../submodules/utilitiec/src/hashmap/hashmap.h"
#include "../submodules/utilitiec/src/StringView/StringView.h"
#include "value.h"
typedef struct ObjectTypeAttribute_s {
StringView name;
enum ValueType type;
uint32_t index;
} ObjectTypeAttribute;
typedef struct ObjectType_s {
hashmap_t attributes; // map of (char* name) -> (ObjectTypeAttribute entry)
StringView name;
} ObjectType;
#endif

31
src/object.h Normal file
View file

@ -0,0 +1,31 @@
/*
* This code is part of the programming language flup
* flup comes with ABSOLUTELY NO WARRANTY and is licensed under AGPL-3.0 or later.
* Copyright (C) 2024 VegOwOtenks
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef FLUP_OBJECT_H
#define FLUP_OBJECT_H
#include "object-type.h"
struct Object_s {
struct Object_s* next; // linked list of all objects
ObjectType* type;
union ValueContent attributes[];
};
#endif

View file

@ -94,6 +94,16 @@ static Token _Tokenizer_IdentifierToken(StringView* source)
}; };
} }
static bool _Tokenizer_ContinueCommentFunction(char c)
{
return c != '\n';
}
static bool _Tokenizer_ContinueStringFunction(char c)
{
return c != '"';
}
static Token _Tokenizer_SimpleToken(StringView* source) static Token _Tokenizer_SimpleToken(StringView* source)
{ {
const char* literal_table[] = { "{", "}", "&", ":", "+", "->", "-", "*", "/", "|", "==", "!=", "<", "<=", ">", ">=", ",", ";", "bind", "as", "(", ")" }; const char* literal_table[] = { "{", "}", "&", ":", "+", "->", "-", "*", "/", "|", "==", "!=", "<", "<=", ">", ">=", ",", ";", "bind", "as", "(", ")" };
@ -135,6 +145,30 @@ static Token _Tokenizer_SimpleToken(StringView* source)
return TOKEN_NONE; return TOKEN_NONE;
} }
Token _Tokenizer_CommentToken(StringView* source)
{
StringView comment = StringView_SpanWhile(source, _Tokenizer_ContinueCommentFunction);
return (Token) {
.type = TOKENTYPE_COMMENT,
.get = { .identifier = comment },
};
}
Token _Tokenizer_StringToken(StringView* source)
{
*source = StringView_Drop(*source, 1);
StringView string = StringView_SpanWhile(source, _Tokenizer_ContinueStringFunction);
string.source--;
string.length += 2;
*source = StringView_Drop(*source, 1);
return (Token) {
.type = TOKENTYPE_STRING,
.get = { .identifier = string },
};
}
Token Tokenizer_NextToken(StringView* source) Token Tokenizer_NextToken(StringView* source)
{ {
while (source->length != 0 && isspace(source->source[0])) { while (source->length != 0 && isspace(source->source[0])) {
@ -158,6 +192,10 @@ Token Tokenizer_NextToken(StringView* source)
} else if (isalpha(source->source[0])) { } else if (isalpha(source->source[0])) {
// parse name // parse name
return _Tokenizer_IdentifierToken(source); return _Tokenizer_IdentifierToken(source);
} else if (StringView_StartsWith(*source, StringView_FromString("#"))) {
return _Tokenizer_CommentToken(source);
} else if (StringView_StartsWith(*source, StringView_FromString("\""))) {
return _Tokenizer_StringToken(source);
} else { } else {
return (Token) {.type = TOKENTYPE_ERROR, .get = {.error = *source } }; return (Token) {.type = TOKENTYPE_ERROR, .get = {.error = *source } };
} }
@ -220,6 +258,10 @@ const char* TokenType_ToString(enum TokenType type)
return "LEFT_PAREN"; return "LEFT_PAREN";
case TOKENTYPE_RIGHT_PAREN: case TOKENTYPE_RIGHT_PAREN:
return "RIGHT_PAREN"; return "RIGHT_PAREN";
case TOKENTYPE_COMMENT:
return "COMMENT";
case TOKENTYPE_STRING:
return "STRING";
} }
return "INVALID"; return "INVALID";

View file

@ -31,6 +31,8 @@ enum TokenType {
TOKENTYPE_AS, TOKENTYPE_AS,
TOKENTYPE_LEFT_PAREN, TOKENTYPE_LEFT_PAREN,
TOKENTYPE_RIGHT_PAREN, TOKENTYPE_RIGHT_PAREN,
TOKENTYPE_COMMENT,
TOKENTYPE_STRING,
TOKENTYPE_ERROR, TOKENTYPE_ERROR,
}; };

34
src/value-content.h Normal file
View file

@ -0,0 +1,34 @@
/*
* This code is part of the programming language flup
* flup comes with ABSOLUTELY NO WARRANTY and is licensed under AGPL-3.0 or later.
* Copyright (C) 2024 VegOwOtenks
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef FLUP_VALUECONTENT_H
#define FLUP_VALUECONTENT_H
#include <stdint.h>
typedef struct Object_s Object;
union ValueContent {
int64_t i64;
double f64;
bool boolean;
Object* object;
};
#endif

View file

@ -29,6 +29,8 @@ bool Value_IsTruthy(Value* v)
return v->get.f64 != 0.0; return v->get.f64 != 0.0;
case VALUETYPE_BOOLEAN: case VALUETYPE_BOOLEAN:
return v->get.boolean; return v->get.boolean;
case VALUETYPE_OBJECT:
return v->get.object != NULL;
} }
return false; return false;
@ -43,7 +45,9 @@ bool Value_Equal(Value* v1, Value* v2)
case VALUETYPE_DOUBLE: case VALUETYPE_DOUBLE:
return v1->get.f64 == v2->get.f64; return v1->get.f64 == v2->get.f64;
case VALUETYPE_BOOLEAN: case VALUETYPE_BOOLEAN:
return v1->get.boolean = v2->get.boolean; return v1->get.boolean == v2->get.boolean;
case VALUETYPE_OBJECT:
return v1->get.object == v2->get.object;
} }
return false; return false;

View file

@ -22,19 +22,18 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include "StringView/StringView.h"
#include "../submodules/utilitiec/src/StringView/StringView.h"
enum ValueType { enum ValueType {
VALUETYPE_INT64, VALUETYPE_INT64,
VALUETYPE_DOUBLE, VALUETYPE_DOUBLE,
VALUETYPE_OBJECT,
VALUETYPE_BOOLEAN, VALUETYPE_BOOLEAN,
}; };
union ValueContent { #include "value-content.h"
int64_t i64; #include "object.h"
double f64;
bool boolean;
};
typedef struct Value_s { typedef struct Value_s {
enum ValueType type; enum ValueType type;

View file

@ -21,7 +21,7 @@
#define FLUP_VARIABLE_H #define FLUP_VARIABLE_H
#include "value.h" #include "value.h"
#include "StringView/StringView.h" #include "../submodules/utilitiec/src/StringView/StringView.h"
typedef struct FlupVariable_s { typedef struct FlupVariable_s {
StringView name; StringView name;

3
test-inputs/comment.flup Normal file
View file

@ -0,0 +1,3 @@
1 2 # -
+
println

1
test-inputs/string.flup Normal file
View file

@ -0,0 +1 @@
"Hello World!" println