More work on grammer and standard library! It can kinda load grammer now! Kinda. Marcus ran into the function pointer returns pointer ambiguity, so that'll have to be done tomorrow.

This commit is contained in:
Nathan Braswell
2015-07-04 03:21:36 -04:00
parent b62c3e729f
commit 54721b4284
11 changed files with 149 additions and 4 deletions

View File

@@ -840,7 +840,9 @@ NodeTree<ASTData>* ASTTransformation::doFunction(NodeTree<ASTData>* scope, std::
std::cout << i->getDataRef()->toString() << " "; std::cout << i->getDataRef()->toString() << " ";
std::cout << std::endl; std::cout << std::endl;
NodeTree<ASTData>* operatorMethod = NULL; NodeTree<ASTData>* operatorMethod = NULL;
if (nodes[0]->getDataRef()->valueType && nodes[0]->getDataRef()->valueType->typeDefinition)
// make sure this isn't a pointer, also. Silly vector<string> bug
if (nodes[0]->getDataRef()->valueType && !nodes[0]->getDataRef()->valueType->getIndirection() && nodes[0]->getDataRef()->valueType->typeDefinition)
operatorMethod = functionLookup(nodes[0]->getDataRef()->valueType->typeDefinition, lookupOp, mapNodesToTypes(slice(nodes,1,-1))); operatorMethod = functionLookup(nodes[0]->getDataRef()->valueType->typeDefinition, lookupOp, mapNodesToTypes(slice(nodes,1,-1)));
if (operatorMethod) { if (operatorMethod) {
//Ok, so we construct //Ok, so we construct

View File

@@ -3,10 +3,54 @@ import vector
import set import set
import symbol import symbol
import regex import regex
import io
fun parse_line(line: string::string): vector::vector<string::string> {
var out.construct(): vector::vector<string>
var begin = 0
for (var i = 1; i < line.length(); i++;) {
if (line[i] == '=') {
i += 2
begin = i
}
if (line[i] == ' ') {
out.add(line.slice(begin, i))
begin = i + 1
}
}
return out
}
fun load_grammer(path: string::string): grammer {
var gram.construct(): grammer
io::read_file(path).lines().for_each(fun(line: string::string) {
if (line.length() == 0)
return;
if (line[0] == '#') {
io::print("comment: "); io::println(line)
return;
}
var parts = parse_line(line)
/*io::print("parts: ")*/
/*parts.for_each(fun(i :string::string){ io::print(i); io::print(" "); })*/
/*io::println()*/
gram.rules.add(rule(symbol::symbol(parts[0], true),
parts.slice(1,-1).map(fun(i: string::string):symbol::symbol {
return symbol::symbol(i, true);
})
))
})
/*gram.rules.add(rule(symbol::symbol("test", true), vector::vector(symbol::symbol("rhs", true))))*/
/*gram.rules.add(rule(symbol::symbol("lhs", true), vector::vector(symbol::symbol("rhs2", true))))*/
/*gram.regexs.add(regex::regex("reg"))*/
return gram
}
obj grammer (Object) { obj grammer (Object) {
var rules: vector::vector<rule> var rules: vector::vector<rule>
var regexs: set::set<regex> var regexs: set::set<regex::regex>
fun construct(): grammer* { fun construct(): grammer* {
rules.construct() rules.construct()
@@ -24,6 +68,21 @@ obj grammer (Object) {
rules.destruct() rules.destruct()
regexs.destruct() regexs.destruct()
} }
fun to_string(): string::string {
var result = string::string("grammer rules:")
rules.for_each( fun(i : rule) { result += string::string("\n\t") + i.to_string(); } )
result += "\nregexs:"
regexs.for_each( fun(i : regex::regex) { result += string::string("\n\t") + i.regexString; } )
return result
}
}
fun rule(lhs: symbol::symbol, rhs: vector::vector<symbol::symbol>): rule {
var toRet.construct(): rule
toRet.lhs = lhs
toRet.rhs = rhs
return toRet
} }
obj rule (Object) { obj rule (Object) {
@@ -38,7 +97,7 @@ obj rule (Object) {
position = 0 position = 0
lookahead.construct() lookahead.construct()
} }
fun copy_construct(old: rule*) { fun copy_construct(other: rule*) {
lhs.copy_construct(&other->lhs) lhs.copy_construct(&other->lhs)
rhs.copy_construct(&other->rhs) rhs.copy_construct(&other->rhs)
position = other->position position = other->position
@@ -53,5 +112,11 @@ obj rule (Object) {
rhs.destruct() rhs.destruct()
lookahead.destruct() lookahead.destruct()
} }
fun to_string(): string::string {
var result = lhs.name + " -> "
rhs.for_each( fun(i : symbol::symbol) { result += i.name + " "; } )
return result
}
} }

View File

@@ -66,6 +66,10 @@ obj regex (Object) {
mem::safe_recursive_delete(begin, fun(it: regexState*): set::set<regexState*> { return set::from_vector(it->next_states); } ) mem::safe_recursive_delete(begin, fun(it: regexState*): set::set<regexState*> { return set::from_vector(it->next_states); } )
} }
fun operator==(other: regex):bool {
return regexString == other.regexString
}
fun operator=(other: regex):void { fun operator=(other: regex):void {
destruct() destruct()
construct(other.regexString) construct(other.regexString)

View File

@@ -81,6 +81,10 @@ obj string (Object) {
return ret return ret
} }
fun operator+=(character: char): void {
data += character
}
fun operator+=(str: char*): void { fun operator+=(str: char*): void {
var newStr.construct(str):string var newStr.construct(str):string
data += newStr.data data += newStr.data
@@ -99,5 +103,20 @@ obj string (Object) {
out[data.size] = 0 out[data.size] = 0
return out; return out;
} }
fun lines(): vector::vector<string> {
var out.construct(): vector::vector<string>
var current = string("")
for (var i = 0; i < data.size; i++;) {
if (data[i] == '\n') {
out.add(current)
current = string("")
} else {
current += data[i]
}
}
out.add(current)
return out
}
}; };

View File

@@ -60,6 +60,10 @@ obj vector<T> (Object) {
return newVec return newVec
} }
fun operator+=(other: T):void {
addEnd(other)
}
fun operator+=(other:vector<T>):void { fun operator+=(other:vector<T>):void {
for (var i = 0; i < other.size; i++;) for (var i = 0; i < other.size; i++;)
addEnd(other.get(i)) addEnd(other.get(i))

4
tests/grammer.kgm Normal file
View File

@@ -0,0 +1,4 @@
# comment
a = b ;
b = "c" ;
b = "c" "d" ;

View File

@@ -1,7 +1,34 @@
import io import io: something
//comments
fun first(): cutomTye {
}
fun another(): cutomTye<hello, beta>* {
}
fun main():int { fun main():int {
if (true) {
thing;
}
if ( 3 < 300) {
}
if ( 3 > 300) {
}
var hastype: type = 20
thingy.other;
if ( 3 < 300) {
}
io::print("hi")
wanna.callit(internal, to ,it)
var a = 2 + 7;
var b = 2 + 7 / 3 - 2 * 7 && 9 || 2 + !g
return 0 return 0
return 11820
return 1.1
return 1.1f
return "hi"
} }

11
tests/test_grammer.krak Normal file
View File

@@ -0,0 +1,11 @@
import io:*
import grammer:*
import string:*
fun main():int {
var a = load_grammer(string("grammer.kgm"))
println(a.to_string())
return 0
}

View File

@@ -0,0 +1,6 @@
fun main():int {
var wr: fun():int*
return 0
}

View File

@@ -9,3 +9,5 @@ a
now win! now win!
true true
false false
2
lines

View File

@@ -22,6 +22,7 @@ fun main(): int {
io::println(newWay) io::println(newWay)
io::println( string::string("yes") == string::string("yes") ) io::println( string::string("yes") == string::string("yes") )
io::println( string::string("no") == string::string("yes") ) io::println( string::string("no") == string::string("yes") )
string::string("2\nlines").lines().for_each(fun(i: string::string) io::println(i);)
return 0; return 0;
} }