New syntax starting to work! Not templates yet, that's next

This commit is contained in:
Nathan Braswell
2015-04-14 16:08:36 -04:00
parent e3aa531856
commit 08431aa748
5 changed files with 52 additions and 30 deletions

View File

@@ -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 ; 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 ; 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 ">" ; template_inst = "<" WS type_list WS ">" ;
type_list = type_list WS "," WS type | type ; 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_list = template_param_list WS "," WS template_param | template_param ;
template_param = identifier WS traits | identifier ; template_param = identifier WS traits | identifier ;
@@ -52,28 +52,22 @@ scoped_identifier = scoped_identifier WS scope_op WS identifier | identifier ;
right_shift = ">" ">" ; right_shift = ">" ">" ;
overloadable_operator = "\+" | "-" | "\*" | "/" | "%" | "^" | "&" | "\|" | "~" | "!" | "," | "=" | "\+\+" | "--" | "<<" | right_shift | "==" | "!=" | "&&" | "\|\|" | "\+=" | "-=" | "/=" | "%=" | "^=" | "&=" | "\|=" | "\*=" | "<<=" | ">>=" | "->" ; overloadable_operator = "\+" | "-" | "\*" | "/" | "%" | "^" | "&" | "\|" | "~" | "!" | "," | "=" | "\+\+" | "--" | "<<" | right_shift | "==" | "!=" | "&&" | "\|\|" | "\+=" | "-=" | "/=" | "%=" | "^=" | "&=" | "\|=" | "\*=" | "<<=" | ">>=" | "->" ;
func_identifier = identifier | identifier overloadable_operator ; 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 | ; opt_typed_parameter_list = typed_parameter_list | ;
typed_parameter_list = typed_parameter_list WS "," WS typed_parameter | typed_parameter ; 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 | ; opt_parameter_list = parameter_list | ;
parameter_list = parameter_list WS "," WS parameter | parameter ; parameter_list = parameter_list WS "," WS parameter | parameter ;
parameter = boolean_expression ; 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 | ; declaration_block = declaration_statement WS SEMI WS declaration_block | function WS declaration_block | declaration_statement WS SEMI | function | ;
traits = "\(" WS trait_list WS "\)" ; traits = "\(" WS trait_list WS "\)" ;
trait_list = trait_list WS "," WS scoped_identifier | scoped_identifier ; 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 ; 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 ; while_loop = "while" WS boolean_expression WS statement ;
@@ -102,7 +96,7 @@ number = integer | floating_literal ;
access_operation = unarad "." identifier | unarad "->" identifier ; 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 ; 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 ; alphanumeric = alphanumeric numeric | alphanumeric alpha | numeric | alpha ;
hexadecimal = "0x(1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)+" ; hexadecimal = "0x(1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)+" ;

View File

@@ -230,12 +230,12 @@ NodeTree<ASTData>* ASTTransformation::secondPassFunction(NodeTree<Symbol>* from,
std::cout << "Finished Non-Instantiated Template function " << functionName << std::endl; std::cout << "Finished Non-Instantiated Template function " << functionName << std::endl;
return functionDef; return functionDef;
} }
functionName = concatSymbolTree(children[1]); functionName = concatSymbolTree(children[0]);
functionDef = new NodeTree<ASTData>("function", ASTData(function, Symbol(functionName, true), typeFromTypeNode(children[0], scope, templateTypeReplacements))); functionDef = new NodeTree<ASTData>("function", ASTData(function, Symbol(functionName, true), typeFromTypeNode(children[children.size()-2], scope, templateTypeReplacements)));
addToScope("~enclosing_scope", scope, functionDef); addToScope("~enclosing_scope", scope, functionDef);
addToScope(functionName, functionDef, scope); addToScope(functionName, functionDef, scope);
//We only do the parameter nodes. We don't do the body yet, as this is the secondPass //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<int>(), functionDef, std::vector<Type>(), templateTypeReplacements); auto transChildren = transformChildren(slice(children,1,-3, 2), std::set<int>(), functionDef, std::vector<Type>(), templateTypeReplacements);
// std::cout << "REGULAR function " << functionName << " has " << transChildren.size() << " parameters: "; // std::cout << "REGULAR function " << functionName << " has " << transChildren.size() << " parameters: ";
// for (auto i : transChildren) // for (auto i : transChildren)
@@ -307,20 +307,21 @@ void ASTTransformation::thirdPass(NodeTree<ASTData>* ast, NodeTree<Symbol>* pars
//This function finds the right AST definition in a scope given its parseTree //This function finds the right AST definition in a scope given its parseTree
NodeTree<ASTData>* ASTTransformation::searchScopeForFunctionDef(NodeTree<ASTData>* scope, NodeTree<Symbol>* parseTree, std::map<std::string, Type*> templateTypeReplacements) { NodeTree<ASTData>* ASTTransformation::searchScopeForFunctionDef(NodeTree<ASTData>* scope, NodeTree<Symbol>* parseTree, std::map<std::string, Type*> templateTypeReplacements) {
std::string functionName = concatSymbolTree(parseTree->getChildren()[1]); std::string functionName = concatSymbolTree(parseTree->getChildren()[0]);
std::vector<Type> types; std::vector<Type> types;
std::vector<NodeTree<Symbol>*> children = parseTree->getChildren(); std::vector<NodeTree<Symbol>*> children = parseTree->getChildren();
//Skipping the initial return type and identifier as well as the final code block //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; std::cout << "\n Searching scope for function def, function is: " << concatSymbolTree(children[0]) << ", children size is " << children.size() << std::endl;
for (int i = 2; i < children.size()-1; i+=2) { //Skip over commas for (int i = 1; i < children.size()-3; i+=2) { //Skip over commas
std::cout << "Making type for lookup ||" << concatSymbolTree(children[i]) << "||" << std::endl; 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; std::cout << "Type made: " << type.toString() << std::endl;
types.push_back(type); 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<ASTData>* result = functionLookup(scope, functionName, types); NodeTree<ASTData>* 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; return result;
} }
@@ -410,7 +411,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
scope = newNode; scope = newNode;
} else if (name == "function") { } else if (name == "function") {
std::string functionName; std::string functionName;
/*MULTHERE*/ //If this is a function template //If this is a function template
if (children[0]->getData().getName() == "template_dec") { if (children[0]->getData().getName() == "template_dec") {
functionName = concatSymbolTree(children[2]); functionName = concatSymbolTree(children[2]);
newNode = new NodeTree<ASTData>(name, ASTData(function, Symbol(functionName, true), new Type(template_type, from))); newNode = new NodeTree<ASTData>(name, ASTData(function, Symbol(functionName, true), new Type(template_type, from)));
@@ -431,12 +432,12 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
std::cout << "Finished Non-Instantiated Template function " << functionName << std::endl; std::cout << "Finished Non-Instantiated Template function " << functionName << std::endl;
return newNode; return newNode;
} }
functionName = concatSymbolTree(children[1]); functionName = concatSymbolTree(children[0]);
for (auto child: children) for (auto child: children)
std::cout << "Function child: " << child->getDataRef()->toString() << std::endl; std::cout << "Function child: " << child->getDataRef()->toString() << std::endl;
newNode = new NodeTree<ASTData>(name, ASTData(function, Symbol(functionName, true), typeFromTypeNode(children[0], scope, templateTypeReplacements))); newNode = new NodeTree<ASTData>(name, ASTData(function, Symbol(functionName, true), typeFromTypeNode(children[children.size()-2], scope, templateTypeReplacements)));
skipChildren.insert(0); skipChildren.insert(0);
skipChildren.insert(1); skipChildren.insert(children.size()-1);
addToScope(functionName, newNode, scope); addToScope(functionName, newNode, scope);
addToScope("~enclosing_scope", scope, newNode); addToScope("~enclosing_scope", scope, newNode);
scope = newNode; scope = newNode;
@@ -456,10 +457,10 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
scope = newNode; scope = newNode;
} else if (name == "typed_parameter") { } else if (name == "typed_parameter") {
//newNode = transform(children[1]); //Transform to get the identifier //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::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 //std::string typeString = concatSymbolTree(children[0]);//Get the type (left child) and set our new identifer to be that type
newNode = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(parameterName, true), typeFromTypeNode(children[0], scope, templateTypeReplacements))); newNode = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(parameterName, true), typeFromTypeNode(children[2], scope, templateTypeReplacements)));
addToScope(parameterName, newNode, scope); addToScope(parameterName, newNode, scope);
addToScope("~enclosing_scope", scope, newNode); addToScope("~enclosing_scope", scope, newNode);
std::cout << "Done doing typed_parameter " << parameterName << std::endl; std::cout << "Done doing typed_parameter " << parameterName << std::endl;
@@ -582,8 +583,8 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
// NodeTree<ASTData>* newIdentifier = transform(children[1], scope); //Transform the identifier // NodeTree<ASTData>* newIdentifier = transform(children[1], scope); //Transform the identifier
// newIdentifier->getDataRef()->valueType = Type(concatSymbolTree(children[0]));//set the type of the identifier // newIdentifier->getDataRef()->valueType = Type(concatSymbolTree(children[0]));//set the type of the identifier
std::string newIdentifierStr = concatSymbolTree(children[1]); std::string newIdentifierStr = concatSymbolTree(children[0]);
Type* identifierType = typeFromTypeNode(children[0], scope, templateTypeReplacements); Type* identifierType = typeFromTypeNode(children[2], scope, templateTypeReplacements);
std::cout << "Declaring an identifier " << newIdentifierStr << " to be of type " << identifierType->toString() << std::endl; std::cout << "Declaring an identifier " << newIdentifierStr << " to be of type " << identifierType->toString() << std::endl;
NodeTree<ASTData>* newIdentifier = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(newIdentifierStr, true), identifierType)); NodeTree<ASTData>* newIdentifier = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(newIdentifierStr, true), identifierType));
addToScope(newIdentifierStr, newIdentifier, scope); addToScope(newIdentifierStr, newIdentifier, scope);
@@ -614,7 +615,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
} }
skipChildren.insert(0); //These, the type and the identifier, have been taken care of. 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)); newNode->addChildren(transformChildren(children, skipChildren, scope, types, templateTypeReplacements));
return newNode; return newNode;
} else if (name == "if_comp") { } else if (name == "if_comp") {

View File

@@ -19,6 +19,9 @@ Importer::Importer(Parser* parserIn, std::vector<std::string> includePaths, std:
removeSymbols.push_back(Symbol("\\)", true)); removeSymbols.push_back(Symbol("\\)", true));
//removeSymbols.push_back(Symbol("::", true)); //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(";", true));
removeSymbols.push_back(Symbol("SEMI", false)); removeSymbols.push_back(Symbol("SEMI", false));
removeSymbols.push_back(Symbol("{", true)); removeSymbols.push_back(Symbol("{", true));

View File

@@ -0,0 +1,2 @@
yeah new syntax: 7, 6
yeah new syntax: 8, 8.000000

22
tests/test_newSyntax.krak Normal file
View File

@@ -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
}