\#cimport implementation

This commit is contained in:
2026-08-04 21:38:21 +10:00
parent 5efb5d168d
commit 376d6ec73b
4 changed files with 108 additions and 3 deletions

View File

@@ -78,6 +78,8 @@ namespace Solstice {
return "GreaterThan";
case NodeType::LesserThan:
return "LesserThan";
case NodeType::CImport:
return "#CImport";
}
throw std::runtime_error("FIXME Unhandled case in nodeTypeToString");
}
@@ -111,6 +113,73 @@ namespace Solstice {
}
return node;
}
Node Parser::parseHashCimport() {
// puts :: #cimport(Int, (String), "puts", ".")
Node node{NodeType::CImport};
auto next = parseOneNode(Precedence::FunctionCall);
if (!next.has_value()) {
throw std::runtime_error("expecing arguments to #cimport");
}
if (next->type != NodeType::Tuple) {
throw std::runtime_error("expecting tuple-like argument list to follow #cimport");
}
if (next->children.size() != 5) {
throw std::runtime_error("cimport takes 5 arguments, that has not been provided");
}
if (next->children[1].type != NodeType::Identifier) {
throw std::runtime_error("expecting an identifier for the function name as the first argument to #cimport");
}
if (next->children[1].type != NodeType::Identifier) {
throw std::runtime_error("expecting an identifier for the return type as the second argument to #cimport");
}
if (next->children[2].type != NodeType::Expression && next->children[2].type != NodeType::Tuple) {
throw std::runtime_error("expecting a tuple (or single expression in brackets) for function argument types");
}
for (const auto& idNode : next->children[2].children) {
if (idNode.type != NodeType::Identifier) {
throw std::runtime_error("expecting identifiers in the tuple of function argument types");
}
}
if (next->children[3].type == NodeType::Literal) {
auto lit = next->children[3].getLiteral();
if (!lit.has_value()) {
throw std::runtime_error("literal does not hold a literal value");
}
auto str = lit->getString();
if (!str.has_value()) {
throw std::runtime_error("expecting string for c function name");
}
} else {
throw std::runtime_error("expecting string for c function name");
}
if (next->children[4].type == NodeType::Literal) {
auto lit = next->children[4].getLiteral();
if (!lit.has_value()) {
throw std::runtime_error("literal does not hold a literal value");
}
auto str = lit->getString();
if (!str.has_value()) {
throw std::runtime_error("expecting string for c library name");
}
} else {
throw std::runtime_error("expecting string for c library name");
}
node.children = next->children;
return node;
}
Node Parser::parseActionFunction() {
Node node{NodeType::FunctionBind};
@@ -417,6 +486,9 @@ namespace Solstice {
case TT::OpenCurly:
return parseOpenCurly();
case TT::Hash_CImport:
return parseHashCimport();
case TT::NewLine:
// ignore new line
return parseOneNode(precedence);

View File

@@ -15,7 +15,8 @@ namespace Solstice {
FunctionBind, Bind, Set, SetType,
Lambda, FunctionCall,
Add, Subtract, Multiply, Divide,
Equal, NotEqual, GreaterThan, LesserThan
Equal, NotEqual, GreaterThan, LesserThan,
CImport
};
enum class Precedence {

View File

@@ -1,8 +1,6 @@
#include "typechecker.hpp"
#include "type.hpp"
#include <functional>
#include <stdexcept>
#include <iostream>
namespace Solstice {
@@ -560,6 +558,32 @@ namespace Solstice {
narrowBinaryNode(node, lesserThanOverloads);
}
void TypeChecker::checkCImportType(Node& node) {
auto name = *node.children[0].getIdentifier();
// Get function's type
Function function;
auto returnTypeId = *node.children[1].getIdentifier();
if (types.find(returnTypeId) == types.end()) {
throw std::runtime_error("Unknown type " + returnTypeId);
}
auto returnType = types[returnTypeId];
std::vector<Type> functionTypes;
for (const auto& arg : node.children[2].children) {
auto argTypeId = *arg.getIdentifier();
if (types.find(argTypeId) == types.end()) {
throw std::runtime_error("Unknown type " + argTypeId);
}
functionTypes.push_back(types[argTypeId]);
}
function.returnTypes[functionTypes] = returnType;
if (functions.find(name) != functions.end()) {
throw std::runtime_error("cannot override function with name " + name);
}
functions[name] = function;
}
void TypeChecker::checkNodeType(Node& node) {
switch (node.type) {
@@ -570,6 +594,7 @@ namespace Solstice {
switch (child.type) {
case NodeType::Bind:
case NodeType::FunctionBind:
case NodeType::CImport:
checkNodeType(child);
break;
default:
@@ -629,7 +654,12 @@ namespace Solstice {
case NodeType::FunctionCall:
checkFunctionCallNodeType(node);
break;
case NodeType::CImport:
checkCImportType(node);
break;
default:
throw std::runtime_error("FIXME unimplemented type checking case");
}
}

View File

@@ -61,6 +61,8 @@ namespace Solstice {
void checkGreaterThanType(Node& node);
void checkLesserThanType(Node& node);
void checkCImportType(Node& node);
void checkNodeType(Node& node);
public: