\#cimport implementation
This commit is contained in:
@@ -78,6 +78,8 @@ namespace Solstice {
|
|||||||
return "GreaterThan";
|
return "GreaterThan";
|
||||||
case NodeType::LesserThan:
|
case NodeType::LesserThan:
|
||||||
return "LesserThan";
|
return "LesserThan";
|
||||||
|
case NodeType::CImport:
|
||||||
|
return "#CImport";
|
||||||
}
|
}
|
||||||
throw std::runtime_error("FIXME Unhandled case in nodeTypeToString");
|
throw std::runtime_error("FIXME Unhandled case in nodeTypeToString");
|
||||||
}
|
}
|
||||||
@@ -111,6 +113,73 @@ namespace Solstice {
|
|||||||
}
|
}
|
||||||
return node;
|
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 Parser::parseActionFunction() {
|
||||||
Node node{NodeType::FunctionBind};
|
Node node{NodeType::FunctionBind};
|
||||||
|
|
||||||
@@ -417,6 +486,9 @@ namespace Solstice {
|
|||||||
case TT::OpenCurly:
|
case TT::OpenCurly:
|
||||||
return parseOpenCurly();
|
return parseOpenCurly();
|
||||||
|
|
||||||
|
case TT::Hash_CImport:
|
||||||
|
return parseHashCimport();
|
||||||
|
|
||||||
case TT::NewLine:
|
case TT::NewLine:
|
||||||
// ignore new line
|
// ignore new line
|
||||||
return parseOneNode(precedence);
|
return parseOneNode(precedence);
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ namespace Solstice {
|
|||||||
FunctionBind, Bind, Set, SetType,
|
FunctionBind, Bind, Set, SetType,
|
||||||
Lambda, FunctionCall,
|
Lambda, FunctionCall,
|
||||||
Add, Subtract, Multiply, Divide,
|
Add, Subtract, Multiply, Divide,
|
||||||
Equal, NotEqual, GreaterThan, LesserThan
|
Equal, NotEqual, GreaterThan, LesserThan,
|
||||||
|
CImport
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class Precedence {
|
enum class Precedence {
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
#include "typechecker.hpp"
|
#include "typechecker.hpp"
|
||||||
#include "type.hpp"
|
#include "type.hpp"
|
||||||
#include <functional>
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
namespace Solstice {
|
namespace Solstice {
|
||||||
|
|
||||||
@@ -560,6 +558,32 @@ namespace Solstice {
|
|||||||
narrowBinaryNode(node, lesserThanOverloads);
|
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) {
|
void TypeChecker::checkNodeType(Node& node) {
|
||||||
switch (node.type) {
|
switch (node.type) {
|
||||||
@@ -570,6 +594,7 @@ namespace Solstice {
|
|||||||
switch (child.type) {
|
switch (child.type) {
|
||||||
case NodeType::Bind:
|
case NodeType::Bind:
|
||||||
case NodeType::FunctionBind:
|
case NodeType::FunctionBind:
|
||||||
|
case NodeType::CImport:
|
||||||
checkNodeType(child);
|
checkNodeType(child);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -629,7 +654,12 @@ namespace Solstice {
|
|||||||
case NodeType::FunctionCall:
|
case NodeType::FunctionCall:
|
||||||
checkFunctionCallNodeType(node);
|
checkFunctionCallNodeType(node);
|
||||||
break;
|
break;
|
||||||
|
case NodeType::CImport:
|
||||||
|
checkCImportType(node);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw std::runtime_error("FIXME unimplemented type checking case");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -61,6 +61,8 @@ namespace Solstice {
|
|||||||
void checkGreaterThanType(Node& node);
|
void checkGreaterThanType(Node& node);
|
||||||
void checkLesserThanType(Node& node);
|
void checkLesserThanType(Node& node);
|
||||||
|
|
||||||
|
void checkCImportType(Node& node);
|
||||||
|
|
||||||
void checkNodeType(Node& node);
|
void checkNodeType(Node& node);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
Reference in New Issue
Block a user