Skip to content

Commit

Permalink
Introduce Deque (double ended queue) std container (#636)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer authored Aug 15, 2024
1 parent d392595 commit 59392c0
Show file tree
Hide file tree
Showing 15 changed files with 352 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .run/spice build.run.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="spice build" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="build -O3 --no-entry --target-arch=wasm32 -ir ../../media/test-project/test.spice" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spice" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spice">
<configuration default="false" name="spice build" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="build -O0 -ir -g ../../media/test-project/test.spice" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spice" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spice">
<envs>
<env name="LLVM_ADDITIONAL_FLAGS" value="-lole32 -lws2_32" />
<env name="LLVM_BUILD_INCLUDE_DIR" value="$PROJECT_DIR$/../llvm-project-latest/build/include" />
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/language/builtins.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Spice offers five builtin functions out of the box. Those can be used anywhere w
Printf works the same as the `printf` function in C and is designed for printing a string to the standard text output (cout).

### Signature
`void printf(string template, ...args)`
`int printf(string template, ...args)`

`template`: Template string, which can contain placeholders for values, passed as args to the `printf` builtin. <br>
`args`: Arbitrary number of arguments of any type. The particular type and the order of the types have to match the placeholders of the template string.
Expand Down
30 changes: 26 additions & 4 deletions media/test-project/test.spice
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
#[core.compiler.mangle = false]
public f<int> fibo(int n) {
if n <= 1 { return n; }
return fibo(n - 1) + fibo(n - 2);
type ITest interface {
p test();
}

type InnerTest struct : ITest {
int a
}

p InnerTest.ctor() {
this.a = 0;
}

p InnerTest.test() {
printf("InnerTest.test()\n");
}

type Test struct {
ITest* test
}

f<int> main() {
InnerTest innerTest;
Test test = Test{&innerTest};
ITest* test2 = &innerTest;
test2.test();
test.test.test();
}
3 changes: 1 addition & 2 deletions src-bootstrap/compiler-pass.spice
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ p CompilerPass.ctor(IGlobalResourceManager& resourceManager, ISourceFile* source
this.resourceManager = resourceManager;
this.cliOptions = resourceManager.cliOptions;
this.sourceFile = sourceFile;
this.rootScope = sourceFile != nil<ISourceFile*> ? sourceFile.globalScope : nil<heap Scope*>;
this.currentScope = sNonOwned(this.rootScope);
this.currentScope = this.rootScope = sourceFile != nil<ISourceFile*> ? sourceFile.globalScope : nil<heap Scope*>;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/global/RuntimeModuleManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const std::unordered_map<const char *, RuntimeModule> FCT_NAME_TO_RT_MODULE_MAPP
{"sNew", MEMORY_RT},
{"sPlacementNew", MEMORY_RT},
{"sDelete", MEMORY_RT},
{"sNonOwned", MEMORY_RT},
{"sCompare", MEMORY_RT},
// Result RT
{"ok", RESULT_RT},
{"err", RESULT_RT},
Expand Down
18 changes: 9 additions & 9 deletions src/typechecker/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ std::any TypeChecker::visitForLoop(ForLoopNode *node) {
visit(node->initDecl());

// Visit condition
QualType conditionType = std::any_cast<ExprResult>(visit(node->condAssign())).type;
const QualType conditionType = std::any_cast<ExprResult>(visit(node->condAssign())).type;
HANDLE_UNRESOLVED_TYPE_PTR(conditionType)
// Check if condition evaluates to bool
if (!conditionType.is(TY_BOOL))
Expand Down Expand Up @@ -281,7 +281,7 @@ std::any TypeChecker::visitDoWhileLoop(DoWhileLoopNode *node) {
visit(node->body());

// Visit condition
QualType conditionType = std::any_cast<ExprResult>(visit(node->condition())).type;
const QualType conditionType = std::any_cast<ExprResult>(visit(node->condition())).type;
HANDLE_UNRESOLVED_TYPE_PTR(conditionType)
// Check if condition evaluates to bool
if (!conditionType.is(TY_BOOL))
Expand All @@ -296,7 +296,7 @@ std::any TypeChecker::visitIfStmt(IfStmtNode *node) {

// Visit condition
AssignExprNode *condition = node->condition();
QualType conditionType = std::any_cast<ExprResult>(visit(condition)).type;
const QualType conditionType = std::any_cast<ExprResult>(visit(condition)).type;
HANDLE_UNRESOLVED_TYPE_PTR(conditionType)
// Check if condition evaluates to bool
if (!conditionType.is(TY_BOOL))
Expand Down Expand Up @@ -339,7 +339,7 @@ std::any TypeChecker::visitElseStmt(ElseStmtNode *node) {
std::any TypeChecker::visitSwitchStmt(SwitchStmtNode *node) {
// Check expression type
AssignExprNode *expr = node->assignExpr();
QualType exprType = std::any_cast<ExprResult>(visit(expr)).type;
const QualType exprType = std::any_cast<ExprResult>(visit(expr)).type;
HANDLE_UNRESOLVED_TYPE_PTR(exprType)
if (!exprType.isOneOf({TY_INT, TY_SHORT, TY_LONG, TY_BYTE, TY_CHAR, TY_BOOL}))
SOFT_ERROR_ER(node->assignExpr(), SWITCH_EXPR_MUST_BE_PRIMITIVE,
Expand All @@ -349,7 +349,7 @@ std::any TypeChecker::visitSwitchStmt(SwitchStmtNode *node) {
visitChildren(node);

// Check if case constant types match switch expression type
for (CaseBranchNode *caseBranchNode : node->caseBranches())
for (const CaseBranchNode *caseBranchNode : node->caseBranches())
for (CaseConstantNode *constantNode : caseBranchNode->caseConstants()) {
const QualType constantType = std::any_cast<ExprResult>(visit(constantNode)).type;
if (!constantType.matches(exprType, false, true, true))
Expand Down Expand Up @@ -807,7 +807,7 @@ std::any TypeChecker::visitPrintfCall(PrintfCallNode *node) {
if (placeholderCount < node->args().size())
SOFT_ERROR_ER(node, PRINTF_ARG_COUNT_ERROR, "The placeholder string contains less placeholders than arguments")

return ExprResult{node->setEvaluatedSymbolType(QualType(TY_BOOL), manIdx)};
return ExprResult{node->setEvaluatedSymbolType(QualType(TY_INT), manIdx)};
}

std::any TypeChecker::visitSizeofCall(SizeofCallNode *node) {
Expand Down Expand Up @@ -962,7 +962,7 @@ std::any TypeChecker::visitTernaryExpr(TernaryExprNode *node) {

// Visit condition
LogicalOrExprNode *condition = node->operands()[0];
QualType conditionType = std::any_cast<ExprResult>(visit(condition)).type;
const QualType conditionType = std::any_cast<ExprResult>(visit(condition)).type;
HANDLE_UNRESOLVED_TYPE_ER(conditionType)

QualType trueType;
Expand Down Expand Up @@ -1793,8 +1793,8 @@ std::any TypeChecker::visitFctCall(FctCallNode *node) {
// Set return type to 'this' type
returnType = data.thisType;
} else if (data.callee->isProcedure()) {
// Procedures always have the return type 'bool'
returnType = QualType(TY_BOOL);
// Procedures always have the return type 'dyn'
returnType = QualType(TY_DYN);
} else {
returnType = data.callee->returnType;
}
Expand Down
Loading

0 comments on commit 59392c0

Please sign in to comment.