Skip to content

Commit

Permalink
remove features in favour of versions
Browse files Browse the repository at this point in the history
  • Loading branch information
yeti0904 committed Apr 21, 2024
1 parent 99f805c commit a11e57b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 78 deletions.
8 changes: 7 additions & 1 deletion source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,13 @@ int main(string[] args) {
auto preproc = new Preprocessor();
preproc.includeDirs = includeDirs;
preproc.versions = versions;
nodes = preproc.Run(nodes);

try {
nodes = preproc.Run(nodes);
}
catch (PreprocessorError) {
return 1;
}

if (optimise) {
auto optimiser = new Optimiser();
Expand Down
26 changes: 1 addition & 25 deletions source/compiler.d
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class CompilerError : Exception {
class Compiler {
CompilerBackend backend;
string[] includeDirs;
string[] features;
string[] included;
string outFile;

Expand Down Expand Up @@ -145,28 +144,6 @@ class Compiler {
break;
}
case NodeType.Let: backend.CompileLet(cast(LetNode) inode); break;
case NodeType.Implements: {
auto node = cast(ImplementsNode) inode;

if (features.canFind(node.feature)) {
CompileNode(node.node);
}
break;
}
case NodeType.Feature: {
auto node = cast(FeatureNode) inode;

features ~= node.feature;
break;
}
case NodeType.Requires: {
auto node = cast(RequiresNode) inode;

if (!features.canFind(node.feature)) {
backend.Error(node.error, "Feature '%s' required", node.feature);
}
break;
}
case NodeType.Array: backend.CompileArray(cast(ArrayNode) inode); break;
case NodeType.String: backend.CompileString(cast(StringNode) inode); break;
case NodeType.Struct: backend.CompileStruct(cast(StructNode) inode); break;
Expand All @@ -190,8 +167,7 @@ class Compiler {
case NodeType.FuncDef:
case NodeType.Include:
case NodeType.Let:
case NodeType.Implements:
case NodeType.Feature:
case NodeType.Enable:
case NodeType.Requires:
case NodeType.Struct:
case NodeType.Const:
Expand Down
17 changes: 2 additions & 15 deletions source/optimiser.d
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,8 @@ class Optimiser {
void Run(Node[] nodes) {
// create function defs first
foreach (ref inode ; nodes) {
if ((inode.type == NodeType.FuncDef) || (inode.type == NodeType.Implements)) {
FuncDefNode node;

if (inode.type == NodeType.Implements) {
auto node2 = (cast(ImplementsNode) inode).node;

if (node2.type != NodeType.FuncDef) {
continue;
}

node = cast(FuncDefNode) node2;
}
else {
node = cast(FuncDefNode) inode;
}
if (inode.type == NodeType.FuncDef) {
FuncDefNode node = cast(FuncDefNode) inode;

if (node.name in functions) {
Error(
Expand Down
48 changes: 11 additions & 37 deletions source/parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ enum NodeType {
If,
While,
Let,
Implements,
Feature,
Enable,
Requires,
Version,
Array,
Expand Down Expand Up @@ -200,27 +199,17 @@ class LetNode : Node {
}
}

class ImplementsNode : Node {
string feature;
Node node;

this(ErrorInfo perror) {
type = NodeType.Implements;
error = perror;
}
}

class FeatureNode : Node {
string feature;
class EnableNode : Node {
string ver;

this(ErrorInfo perror) {
type = NodeType.Feature;
type = NodeType.Enable;
error = perror;
}
}

class RequiresNode : Node {
string feature;
string ver;

this(ErrorInfo perror) {
type = NodeType.Requires;
Expand Down Expand Up @@ -551,27 +540,13 @@ class Parser {
return ret;
}

Node ParseImplements() {
auto ret = new ImplementsNode(GetError());
parsing = NodeType.Implements;
Node ParseEnable() {
auto ret = new EnableNode(GetError());
parsing = NodeType.Enable;

Next();
Expect(TokenType.Identifier);
ret.feature = tokens[i].contents;

Next();
ret.node = ParseStatement();

return ret;
}

Node ParseFeature() {
auto ret = new FeatureNode(GetError());
parsing = NodeType.Feature;

Next();
Expect(TokenType.Identifier);
ret.feature = tokens[i].contents;
ret.ver = tokens[i].contents;

return ret;
}
Expand All @@ -582,7 +557,7 @@ class Parser {

Next();
Expect(TokenType.Identifier);
ret.feature = tokens[i].contents;
ret.ver = tokens[i].contents;

return ret;
}
Expand Down Expand Up @@ -764,8 +739,7 @@ class Parser {
case "if": return ParseIf();
case "while": return ParseWhile();
case "let": return ParseLet();
case "implements": return ParseImplements();
case "feature": return ParseFeature();
case "enable": return ParseEnable();
case "requires": return ParseRequires();
case "struct": return ParseStruct();
case "version": return ParseVersion();
Expand Down
28 changes: 28 additions & 0 deletions source/preprocessor.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,23 @@ import callisto.error;
import callisto.parser;
import callisto.language;

class PreprocessorError : Exception {
this() {
super("", "", 0);
}
}

class Preprocessor {
string[] includeDirs;
string[] included;
string[] versions;

final void Error(Char, A...)(ErrorInfo error, in Char[] fmt, A args) {
ErrorBegin(error);
stderr.writeln(format(fmt, args));
throw new PreprocessorError();
}

Node[] Run(Node[] nodes) {
Node[] ret;

Expand Down Expand Up @@ -60,6 +72,22 @@ class Preprocessor {
}
break;
}
case NodeType.Enable: {
auto node = cast(EnableNode) inode;

if (!versions.canFind(node.ver)) {
versions ~= node.ver;
}
break;
}
case NodeType.Requires: {
auto node = cast(RequiresNode) inode;

if (!versions.canFind(node.ver)) {
Error(node.error, "Version '%s' required", node.ver);
}
break;
}
default: {
ret ~= inode;
}
Expand Down

0 comments on commit a11e57b

Please sign in to comment.