From 08431aa748462ab84394d77c21739ab72aa7772d Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Tue, 14 Apr 2015 16:08:36 -0400 Subject: [PATCH] New syntax starting to work! Not templates yet, that's next --- krakenGrammer.kgm | 18 +++++-------- src/ASTTransformation.cpp | 37 ++++++++++++++------------- src/Importer.cpp | 3 +++ tests/test_newSyntax.expected_results | 2 ++ tests/test_newSyntax.krak | 22 ++++++++++++++++ 5 files changed, 52 insertions(+), 30 deletions(-) create mode 100644 tests/test_newSyntax.expected_results create mode 100644 tests/test_newSyntax.krak diff --git a/krakenGrammer.kgm b/krakenGrammer.kgm index 57e4455..56d8601 100644 --- a/krakenGrammer.kgm +++ b/krakenGrammer.kgm @@ -3,12 +3,12 @@ translation_unit = WS unorderd_list_part WS ; unorderd_list_part = import WS unorderd_list_part | function WS unorderd_list_part | type_def WS SEMI WS unorderd_list_part | if_comp WS unorderd_list_part | simple_passthrough WS unorderd_list_part | declaration_statement WS SEMI WS unorderd_list_part | import | function | type_def WS SEMI | if_comp | simple_passthrough | declaration_statement WS SEMI ; type = type WS "\*" | "void" | "int" | "float" | "double" | "char" | scoped_identifier | scoped_identifier WS template_inst ; -dec_type = "\|" WS type WS "\|" ; +dec_type = ":" WS type ; template_inst = "<" WS type_list WS ">" ; type_list = type_list WS "," WS type | type ; -template_dec = "template" WS "<" WS template_param_list WS ">" ; +template_dec = "<" WS template_param_list WS ">" ; template_param_list = template_param_list WS "," WS template_param | template_param ; template_param = identifier WS traits | identifier ; @@ -52,28 +52,22 @@ scoped_identifier = scoped_identifier WS scope_op WS identifier | identifier ; right_shift = ">" ">" ; overloadable_operator = "\+" | "-" | "\*" | "/" | "%" | "^" | "&" | "\|" | "~" | "!" | "," | "=" | "\+\+" | "--" | "<<" | right_shift | "==" | "!=" | "&&" | "\|\|" | "\+=" | "-=" | "/=" | "%=" | "^=" | "&=" | "\|=" | "\*=" | "<<=" | ">>=" | "->" ; func_identifier = identifier | identifier overloadable_operator ; -function = template_dec WS dec_type WS func_identifier WS "\(" WS opt_typed_parameter_list WS "\)" WS code_block | dec_type WS func_identifier WS "\(" WS opt_typed_parameter_list WS "\)" WS code_block ; +function = "fun" WS func_identifier WS template_dec WS "\(" WS opt_typed_parameter_list WS "\)" WS dec_type WS code_block | "fun" WS func_identifier WS "\(" WS opt_typed_parameter_list WS "\)" WS dec_type WS code_block ; opt_typed_parameter_list = typed_parameter_list | ; typed_parameter_list = typed_parameter_list WS "," WS typed_parameter | typed_parameter ; -typed_parameter = dec_type WS identifier ; +typed_parameter = identifier WS dec_type ; opt_parameter_list = parameter_list | ; parameter_list = parameter_list WS "," WS parameter | parameter ; parameter = boolean_expression ; -type_def = "typedef" WS identifier WS type | "typedef" WS template_dec WS identifier WS "{" WS declaration_block WS "}" | "typedef" WS identifier WS "{" WS declaration_block WS "}" | "typedef" WS template_dec WS identifier WS traits WS "{" WS declaration_block WS "}" | "typedef" WS identifier WS traits WS "{" WS declaration_block WS "}" ; +type_def = "typedef" WS identifier WS type | "typedef" WS identifier WS template_dec WS "{" WS declaration_block WS "}" | "typedef" WS identifier WS "{" WS declaration_block WS "}" | "typedef" WS identifier WS template_dec WS traits WS "{" WS declaration_block WS "}" | "typedef" WS identifier WS traits WS "{" WS declaration_block WS "}" ; declaration_block = declaration_statement WS SEMI WS declaration_block | function WS declaration_block | declaration_statement WS SEMI | function | ; traits = "\(" WS trait_list WS "\)" ; trait_list = trait_list WS "," WS scoped_identifier | scoped_identifier ; -#Older rule for stuff with visibility labels - this should be added sometime -#type_def = "typedef" WS identifier WS type | "typedef" WS template_dec WS identifier WS "{" WS class_innerds WS "}" | "typedef" WS identifier WS "{" WS class_innerds WS "}" | "typedef" WS template_dec WS identifier WS "{" WS declaration_block WS "}" | "typedef" WS identifier WS "{" WS declaration_block WS "}" ; -#class_innerds = visibility_block WS class_innerds | visibility_block ; -#visibility_block = "public:" WS declaration_block | "protected:" WS declaration_block | "private:" WS declaration_block ; - - if_statement = "if" WS "\(" WS boolean_expression WS "\)" WS statement | "if" WS "\(" WS boolean_expression WS "\)" WS statement WS "else" WS statement ; while_loop = "while" WS boolean_expression WS statement ; @@ -102,7 +96,7 @@ number = integer | floating_literal ; access_operation = unarad "." identifier | unarad "->" identifier ; assignment_statement = factor WS "=" WS boolean_expression | factor WS "\+=" WS boolean_expression | factor WS "-=" WS boolean_expression | factor WS "\*=" WS boolean_expression | factor WS "/=" WS boolean_expression ; -declaration_statement = dec_type WS identifier WS "=" WS boolean_expression | dec_type WS identifier | dec_type WS identifier WS "." WS identifier WS "\(" WS opt_parameter_list WS "\)" ; +declaration_statement = "var" WS identifier WS dec_type WS "=" WS boolean_expression | "var" WS identifier WS dec_type | "var" WS identifier WS "." WS identifier WS "\(" WS opt_parameter_list WS "\)" WS dec_type ; alphanumeric = alphanumeric numeric | alphanumeric alpha | numeric | alpha ; hexadecimal = "0x(1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)+" ; diff --git a/src/ASTTransformation.cpp b/src/ASTTransformation.cpp index 6a605e0..edc22a8 100644 --- a/src/ASTTransformation.cpp +++ b/src/ASTTransformation.cpp @@ -230,12 +230,12 @@ NodeTree* ASTTransformation::secondPassFunction(NodeTree* from, std::cout << "Finished Non-Instantiated Template function " << functionName << std::endl; return functionDef; } - functionName = concatSymbolTree(children[1]); - functionDef = new NodeTree("function", ASTData(function, Symbol(functionName, true), typeFromTypeNode(children[0], scope, templateTypeReplacements))); + functionName = concatSymbolTree(children[0]); + functionDef = new NodeTree("function", ASTData(function, Symbol(functionName, true), typeFromTypeNode(children[children.size()-2], scope, templateTypeReplacements))); addToScope("~enclosing_scope", scope, functionDef); addToScope(functionName, functionDef, scope); //We only do the parameter nodes. We don't do the body yet, as this is the secondPass - auto transChildren = transformChildren(slice(children,2,-2), std::set(), functionDef, std::vector(), templateTypeReplacements); + auto transChildren = transformChildren(slice(children,1,-3, 2), std::set(), functionDef, std::vector(), templateTypeReplacements); // std::cout << "REGULAR function " << functionName << " has " << transChildren.size() << " parameters: "; // for (auto i : transChildren) @@ -307,20 +307,21 @@ void ASTTransformation::thirdPass(NodeTree* ast, NodeTree* pars //This function finds the right AST definition in a scope given its parseTree NodeTree* ASTTransformation::searchScopeForFunctionDef(NodeTree* scope, NodeTree* parseTree, std::map templateTypeReplacements) { - std::string functionName = concatSymbolTree(parseTree->getChildren()[1]); + std::string functionName = concatSymbolTree(parseTree->getChildren()[0]); std::vector types; std::vector*> children = parseTree->getChildren(); //Skipping the initial return type and identifier as well as the final code block - std::cout << "\n Searching scope for function def, function is :" << concatSymbolTree(children[1]) << ", children size is " << children.size() << std::endl; - for (int i = 2; i < children.size()-1; i+=2) { //Skip over commas + std::cout << "\n Searching scope for function def, function is: " << concatSymbolTree(children[0]) << ", children size is " << children.size() << std::endl; + for (int i = 1; i < children.size()-3; i+=2) { //Skip over commas std::cout << "Making type for lookup ||" << concatSymbolTree(children[i]) << "||" << std::endl; - Type type = *typeFromTypeNode(children[i]->getChildren()[0], scope, templateTypeReplacements); + Type type = *typeFromTypeNode(children[i]->getChildren().back(), scope, templateTypeReplacements); + //Type type = *typeFromTypeNode(children[i]->getChildren()[0], scope, templateTypeReplacements); std::cout << "Type made: " << type.toString() << std::endl; types.push_back(type); } - std::cout << "About to search scope about " << concatSymbolTree(children[1]) << std::endl; + std::cout << "About to search scope about " << concatSymbolTree(children[0]) << std::endl; NodeTree* result = functionLookup(scope, functionName, types); - std::cout << "Done searching scope about " << concatSymbolTree(children[1]) << std::endl; + std::cout << "Done searching scope about " << concatSymbolTree(children[0]) << std::endl; return result; } @@ -410,7 +411,7 @@ NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree scope = newNode; } else if (name == "function") { std::string functionName; -/*MULTHERE*/ //If this is a function template + //If this is a function template if (children[0]->getData().getName() == "template_dec") { functionName = concatSymbolTree(children[2]); newNode = new NodeTree(name, ASTData(function, Symbol(functionName, true), new Type(template_type, from))); @@ -431,12 +432,12 @@ NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree std::cout << "Finished Non-Instantiated Template function " << functionName << std::endl; return newNode; } - functionName = concatSymbolTree(children[1]); + functionName = concatSymbolTree(children[0]); for (auto child: children) std::cout << "Function child: " << child->getDataRef()->toString() << std::endl; - newNode = new NodeTree(name, ASTData(function, Symbol(functionName, true), typeFromTypeNode(children[0], scope, templateTypeReplacements))); + newNode = new NodeTree(name, ASTData(function, Symbol(functionName, true), typeFromTypeNode(children[children.size()-2], scope, templateTypeReplacements))); skipChildren.insert(0); - skipChildren.insert(1); + skipChildren.insert(children.size()-1); addToScope(functionName, newNode, scope); addToScope("~enclosing_scope", scope, newNode); scope = newNode; @@ -456,10 +457,10 @@ NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree scope = newNode; } else if (name == "typed_parameter") { //newNode = transform(children[1]); //Transform to get the identifier - std::string parameterName = concatSymbolTree(children[1]); + std::string parameterName = concatSymbolTree(children[0]); std::cout << "Doing typed parameter " << parameterName << std::endl; //std::string typeString = concatSymbolTree(children[0]);//Get the type (left child) and set our new identifer to be that type - newNode = new NodeTree("identifier", ASTData(identifier, Symbol(parameterName, true), typeFromTypeNode(children[0], scope, templateTypeReplacements))); + newNode = new NodeTree("identifier", ASTData(identifier, Symbol(parameterName, true), typeFromTypeNode(children[2], scope, templateTypeReplacements))); addToScope(parameterName, newNode, scope); addToScope("~enclosing_scope", scope, newNode); std::cout << "Done doing typed_parameter " << parameterName << std::endl; @@ -582,8 +583,8 @@ NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree // NodeTree* newIdentifier = transform(children[1], scope); //Transform the identifier // newIdentifier->getDataRef()->valueType = Type(concatSymbolTree(children[0]));//set the type of the identifier - std::string newIdentifierStr = concatSymbolTree(children[1]); - Type* identifierType = typeFromTypeNode(children[0], scope, templateTypeReplacements); + std::string newIdentifierStr = concatSymbolTree(children[0]); + Type* identifierType = typeFromTypeNode(children[2], scope, templateTypeReplacements); std::cout << "Declaring an identifier " << newIdentifierStr << " to be of type " << identifierType->toString() << std::endl; NodeTree* newIdentifier = new NodeTree("identifier", ASTData(identifier, Symbol(newIdentifierStr, true), identifierType)); addToScope(newIdentifierStr, newIdentifier, scope); @@ -614,7 +615,7 @@ NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree } skipChildren.insert(0); //These, the type and the identifier, have been taken care of. - skipChildren.insert(1); + skipChildren.insert(2); newNode->addChildren(transformChildren(children, skipChildren, scope, types, templateTypeReplacements)); return newNode; } else if (name == "if_comp") { diff --git a/src/Importer.cpp b/src/Importer.cpp index c4c7fe9..8d04470 100644 --- a/src/Importer.cpp +++ b/src/Importer.cpp @@ -19,6 +19,9 @@ Importer::Importer(Parser* parserIn, std::vector includePaths, std: removeSymbols.push_back(Symbol("\\)", true)); //removeSymbols.push_back(Symbol("::", true)); //removeSymbols.push_back(Symbol(":", true)); + removeSymbols.push_back(Symbol("var", true)); + removeSymbols.push_back(Symbol("fun", true)); + removeSymbols.push_back(Symbol(";", true)); removeSymbols.push_back(Symbol("SEMI", false)); removeSymbols.push_back(Symbol("{", true)); diff --git a/tests/test_newSyntax.expected_results b/tests/test_newSyntax.expected_results new file mode 100644 index 0000000..251ae38 --- /dev/null +++ b/tests/test_newSyntax.expected_results @@ -0,0 +1,2 @@ +yeah new syntax: 7, 6 +yeah new syntax: 8, 8.000000 diff --git a/tests/test_newSyntax.krak b/tests/test_newSyntax.krak new file mode 100644 index 0000000..2cca6b2 --- /dev/null +++ b/tests/test_newSyntax.krak @@ -0,0 +1,22 @@ + +fun withParams(a: int, b: float) : void { + simple_passthrough(a = a, b = b::) """ + printf("yeah new syntax: %d, %f\n", a, b); + """ +} + + +fun main() : int { + var i: int = 7; + var j: int = 6; + simple_passthrough(i = i, j = j::) """ + printf("yeah new syntax: %d, %d\n", i, j); + """ + var a: int = 8 + var b: float = 8 + withParams(a,b) + return 0 +} + + +