some tests failing because things have been made reference in vector, but grammer actually generates the state set for the real grammer in 2 minutes or so after a day of profiling and bugfixing, so this is gonna be committed.
This commit is contained in:
@@ -61,7 +61,7 @@ identifier = augmented_alpha_alphanumeric ;
|
|||||||
scope_op = ":" ":" ;
|
scope_op = ":" ":" ;
|
||||||
scoped_identifier = scoped_identifier WS scope_op WS identifier | identifier ;
|
scoped_identifier = scoped_identifier WS scope_op WS identifier | identifier ;
|
||||||
|
|
||||||
#Note that to prevent confilct with nested templates (T<A<B>>) it is a nonterminal contructed as follows
|
#Note that to prevent confilct with nested templates (T<A<B>>) right_shift is a nonterminal contructed as follows
|
||||||
right_shift = ">" ">" ;
|
right_shift = ">" ">" ;
|
||||||
overloadable_operator = "\+" | "-" | "\*" | "/" | "%" | "^" | "&" | "\|" | "~" | "!" | "," | "=" | "\+\+" | "--" | "<<" | right_shift | "==" | "!=" | "&&" | "\|\|" | "\+=" | "-=" | "/=" | "%=" | "^=" | "&=" | "\|=" | "\*=" | "<<=" | ">>=" | "->" | "\(" "\)" | "[]" | "[]=" ;
|
overloadable_operator = "\+" | "-" | "\*" | "/" | "%" | "^" | "&" | "\|" | "~" | "!" | "," | "=" | "\+\+" | "--" | "<<" | right_shift | "==" | "!=" | "&&" | "\|\|" | "\+=" | "-=" | "/=" | "%=" | "^=" | "&=" | "\|=" | "\*=" | "<<=" | ">>=" | "->" | "\(" "\)" | "[]" | "[]=" ;
|
||||||
func_identifier = identifier | identifier overloadable_operator ;
|
func_identifier = identifier | identifier overloadable_operator ;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ CGenerator::~CGenerator() {
|
|||||||
// Note the use of std::pair to hold two strings - the running string for the header file and the running string for the c file.
|
// Note the use of std::pair to hold two strings - the running string for the header file and the running string for the c file.
|
||||||
void CGenerator::generateCompSet(std::map<std::string, NodeTree<ASTData>*> ASTs, std::string outputName) {
|
void CGenerator::generateCompSet(std::map<std::string, NodeTree<ASTData>*> ASTs, std::string outputName) {
|
||||||
//Generate an entire set of files
|
//Generate an entire set of files
|
||||||
std::string buildString = "#!/bin/sh\ncc -g -std=c99 ";
|
std::string buildString = "#!/bin/sh\ncc -g -O3 -std=c99 ";
|
||||||
std::cout << "\n\n =====GENERATE PASS===== \n\n" << std::endl;
|
std::cout << "\n\n =====GENERATE PASS===== \n\n" << std::endl;
|
||||||
std::cout << "\n\nGenerate pass for: " << outputName << std::endl;
|
std::cout << "\n\nGenerate pass for: " << outputName << std::endl;
|
||||||
buildString += outputName + ".c ";
|
buildString += outputName + ".c ";
|
||||||
|
|||||||
@@ -112,7 +112,11 @@ void Parser::createStateSet() {
|
|||||||
std::queue<State*> toDo;
|
std::queue<State*> toDo;
|
||||||
toDo.push(zeroState);
|
toDo.push(zeroState);
|
||||||
//std::cout << "Begining for main set for loop" << std::endl;
|
//std::cout << "Begining for main set for loop" << std::endl;
|
||||||
|
int count = 0;
|
||||||
while (toDo.size()) {
|
while (toDo.size()) {
|
||||||
|
if (count % 200 == 0)
|
||||||
|
std::cout << "while count: " << count << std::endl;
|
||||||
|
count++;
|
||||||
//closure
|
//closure
|
||||||
closure(toDo.front());
|
closure(toDo.front());
|
||||||
//Add the new states
|
//Add the new states
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import string
|
import string
|
||||||
import vector
|
import vector
|
||||||
import set
|
import set
|
||||||
|
import stack
|
||||||
import map
|
import map
|
||||||
import symbol
|
import symbol
|
||||||
import regex
|
import regex
|
||||||
@@ -176,90 +177,112 @@ obj grammer (Object) {
|
|||||||
|
|
||||||
fun calculate_state_automaton() {
|
fun calculate_state_automaton() {
|
||||||
state_automata.items = vector::vector(rules[0].with_lookahead(set::set(symbol::eof_symbol())))
|
state_automata.items = vector::vector(rules[0].with_lookahead(set::set(symbol::eof_symbol())))
|
||||||
|
io::println("pre first closure")
|
||||||
state_automata = closure(state_automata)
|
state_automata = closure(state_automata)
|
||||||
var states = set::set(state_automata)
|
io::println("post first closure")
|
||||||
var newItem = true
|
var states = vector::vector(state_automata) // vector instead of set because we need to iterate by index
|
||||||
while (newItem) {
|
var newItems = stack::stack(0) // 0 is the index of the first and only item in states
|
||||||
newItem = false
|
var count = 0
|
||||||
states.for_each(fun(I: state) {
|
while (newItems.size()) {
|
||||||
var possGoto = set::set<symbol::symbol>()
|
if (count%200 == 0) {
|
||||||
I.items.for_each(fun(r: rule) {
|
io::print("calculate_state_automaton while")
|
||||||
if (!r.at_end())
|
io::println(count)
|
||||||
possGoto.add(r.next())
|
}
|
||||||
})
|
count++
|
||||||
possGoto.for_each(fun(X: symbol::symbol) {
|
var I = newItems.pop()
|
||||||
var goneState = goto(I, X)
|
var possGoto = set::set<symbol::symbol>()
|
||||||
if (goneState.items.size && !states.contains(goneState)) {
|
states[I].items.for_each(fun(r: ref rule) {
|
||||||
states.add(goneState)
|
if (!r.at_end())
|
||||||
newItem = true
|
possGoto.add(r.next())
|
||||||
}
|
})
|
||||||
})
|
possGoto.for_each(fun(X: ref symbol::symbol) {
|
||||||
|
var goneState = goto(states[I], X)
|
||||||
|
if (goneState.items.size && !states.contains(goneState)) {
|
||||||
|
newItems.push(states.size)
|
||||||
|
states.add(goneState)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
io::println("ALL STATES:\n")
|
io::println("ALL STATES:\n")
|
||||||
states.for_each(fun(i: state) {
|
states.for_each(fun(i: ref state) {
|
||||||
io::println("STATE:\n")
|
io::println("STATE:\n")
|
||||||
i.items.for_each(fun(r: rule) {
|
i.items.for_each(fun(r: ref rule) {
|
||||||
io::println(string::string("\t") + r.to_string())
|
io::println(string::string("\t") + r.to_string())
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
io::println(" there were : states")
|
||||||
|
io::println(states.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun closure(initial: state): state {
|
fun closure(initial: ref state): state {
|
||||||
initial.items = closure(initial.items)
|
initial.items = closure(initial.items)
|
||||||
return initial
|
return initial
|
||||||
}
|
}
|
||||||
fun closure(initial: vector::vector<rule>): vector::vector<rule> {
|
fun closure(initial: ref vector::vector<rule>): vector::vector<rule> {
|
||||||
var continueIt = true
|
var continueIt = true
|
||||||
var count = 0
|
//var count = 0
|
||||||
while (continueIt) {
|
while (continueIt) {
|
||||||
io::print("while")
|
//io::print("closure while")
|
||||||
io::println(count)
|
//io::println(count)
|
||||||
count++
|
//count++
|
||||||
continueIt = false
|
continueIt = false
|
||||||
initial.for_each(fun(i: rule) {
|
for (var i = 0; i < initial.size; i++;) {
|
||||||
if (i.at_end()) {
|
if (initial[i].at_end()) {
|
||||||
return; // continue the for-each
|
continue
|
||||||
}
|
}
|
||||||
rules.for_each(fun(r: rule) {
|
rules.for_each(fun(r: ref rule) {
|
||||||
// if i is |a::=c . Bb, a|, we're doing each B::=... in rules
|
// if i is |a::=c . Bb, a|, we're doing each B::=... in rules
|
||||||
if (r.lhs != i.next())
|
if (r.lhs != initial[i].next())
|
||||||
return // continuing rule-for_each
|
return // continue the for-each
|
||||||
// add r with lookahead
|
// add r with lookahead
|
||||||
var newLookahead = first_vector(i.after_next())
|
var newLookahead = first_vector(initial[i].after_next())
|
||||||
if (newLookahead.contains(symbol::null_symbol())) {
|
if (newLookahead.contains(symbol::null_symbol())) {
|
||||||
newLookahead.remove(symbol::null_symbol())
|
newLookahead.remove(symbol::null_symbol())
|
||||||
newLookahead.add(i.lookahead)
|
newLookahead.add(initial[i].lookahead)
|
||||||
}
|
}
|
||||||
|
var alreadyInInSomeForm = false
|
||||||
for (var index = 0; index < initial.size; index++;) {
|
for (var index = 0; index < initial.size; index++;) {
|
||||||
if (initial[index].equals_but_lookahead(r) && !initial[index].lookahead.contains(newLookahead)) {
|
if (initial[index].equals_but_lookahead(r)) {
|
||||||
//io::println(initial[index].to_string())
|
alreadyInInSomeForm = true
|
||||||
//io::println("and")
|
if (!initial[index].lookahead.contains(newLookahead)) {
|
||||||
//io::println(r.to_string())
|
//io::println("\n\n\n")
|
||||||
//io::println("are the same with different lookaheads")
|
//io::println(initial[index].to_string())
|
||||||
initial[index].lookahead += newLookahead
|
//io::println("and")
|
||||||
continueIt = true
|
//io::println(r.to_string())
|
||||||
//io::println("contineu because equal_but_different")
|
//io::println("with")
|
||||||
return // continuing rule-for_each
|
//var result = string::string("|lookahead {")
|
||||||
|
//newLookahead.for_each(fun(i: symbol::symbol) {
|
||||||
|
//result += i.to_string()
|
||||||
|
//})
|
||||||
|
//io::println(result)
|
||||||
|
//io::println("are the same with different lookaheads")
|
||||||
|
initial[index].lookahead += newLookahead
|
||||||
|
//io::println("so now it's")
|
||||||
|
//io::println(initial[index].to_string())
|
||||||
|
//io::println("contineu because equal_but_different")
|
||||||
|
continueIt = true
|
||||||
|
return // continue the rules for-each
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var newRule = r.with_lookahead(newLookahead)
|
if (!alreadyInInSomeForm) {
|
||||||
if (!initial.contains(newRule)) {
|
|
||||||
continueIt = true
|
continueIt = true
|
||||||
|
//io::println("\n\n\n")
|
||||||
//io::println("contineu because not contains")
|
//io::println("contineu because not contains")
|
||||||
initial.add(newRule)
|
//io::println(newRule.to_string())
|
||||||
|
initial.add(r.with_lookahead(newLookahead))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
return initial
|
return initial
|
||||||
}
|
}
|
||||||
|
|
||||||
fun goto(I: state, X: symbol::symbol): state {
|
fun goto(I: ref state, X: ref symbol::symbol): state {
|
||||||
// loop through i, find all that have thing::= something . X more,
|
// loop through i, find all that have thing::= something . X more,
|
||||||
// add thing ::= something X . more
|
// add thing ::= something X . more
|
||||||
var jPrime = vector::vector<rule>()
|
var jPrime = vector::vector<rule>()
|
||||||
I.items.for_each(fun(i: rule) {
|
I.items.for_each(fun(i: ref rule) {
|
||||||
if (!i.at_end() && i.next() == X)
|
if (!i.at_end() && i.next() == X)
|
||||||
jPrime.add(i.advanced())
|
jPrime.add(i.advanced())
|
||||||
})
|
})
|
||||||
@@ -323,7 +346,7 @@ obj rule (Object) {
|
|||||||
lookahead.destruct()
|
lookahead.destruct()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun next(): symbol::symbol {
|
fun next(): ref symbol::symbol {
|
||||||
return rhs[position]
|
return rhs[position]
|
||||||
}
|
}
|
||||||
fun after_next(): vector::vector<symbol::symbol> {
|
fun after_next(): vector::vector<symbol::symbol> {
|
||||||
|
|||||||
@@ -71,6 +71,9 @@ obj set<T> (Object) {
|
|||||||
}
|
}
|
||||||
data.remove(idx)
|
data.remove(idx)
|
||||||
}
|
}
|
||||||
|
fun for_each(func: fun(ref T):void) {
|
||||||
|
data.for_each(func)
|
||||||
|
}
|
||||||
fun for_each(func: fun(T):void) {
|
fun for_each(func: fun(T):void) {
|
||||||
data.for_each(func)
|
data.for_each(func)
|
||||||
}
|
}
|
||||||
|
|||||||
43
stdlib/stack.krak
Normal file
43
stdlib/stack.krak
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import vector
|
||||||
|
|
||||||
|
|
||||||
|
fun stack<T>():stack<T> {
|
||||||
|
var out.construct():stack<T>
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
fun stack<T>(in:T):stack<T> {
|
||||||
|
var out.construct():stack<T>
|
||||||
|
out.push(in)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
obj stack<T> (Object) {
|
||||||
|
var data: vector::vector<T>
|
||||||
|
fun construct(): *stack<T> {
|
||||||
|
data.construct()
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
fun copy_construct(other: *stack<T>) {
|
||||||
|
data.copy_construct(&other->data)
|
||||||
|
}
|
||||||
|
fun destruct() {
|
||||||
|
data.destruct()
|
||||||
|
}
|
||||||
|
fun operator=(other: ref stack<T>) {
|
||||||
|
data = other.data
|
||||||
|
}
|
||||||
|
fun push(it: ref T) {
|
||||||
|
data.addEnd(it)
|
||||||
|
}
|
||||||
|
fun pop(): T {
|
||||||
|
var toRet = data[data.size-1]
|
||||||
|
data.remove(data.size-1)
|
||||||
|
return toRet
|
||||||
|
}
|
||||||
|
fun top(): T {
|
||||||
|
return data[data.size-1]
|
||||||
|
}
|
||||||
|
fun size(): int {
|
||||||
|
return data.size
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -121,19 +121,19 @@ obj vector<T> (Object) {
|
|||||||
// on an object that we want to put in a vector. In this way we avoid the problem
|
// on an object that we want to put in a vector. In this way we avoid the problem
|
||||||
// by not generating this function unless it's called - we also get the ability to
|
// by not generating this function unless it's called - we also get the ability to
|
||||||
// do a find index using a different type, which could be fun.
|
// do a find index using a different type, which could be fun.
|
||||||
fun find<U>(value: U): int {
|
fun find<U>(value: ref U): int {
|
||||||
for (var i = 0; i < size; i++;)
|
for (var i = 0; i < size; i++;)
|
||||||
if (data[i] == value)
|
if (data[i] == value)
|
||||||
return i;
|
return i;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
// ditto
|
// ditto
|
||||||
fun contains<U>(item: U): bool {
|
fun contains<U>(item: ref U): bool {
|
||||||
return find(item) != -1
|
return find(item) != -1
|
||||||
}
|
}
|
||||||
|
|
||||||
// yep
|
// yep
|
||||||
fun operator==<U>(other:vector<U>):bool {
|
fun operator==<U>(other: ref vector<U>):bool {
|
||||||
if (size != other.size)
|
if (size != other.size)
|
||||||
return false
|
return false
|
||||||
for (var i = 0; i < size; i++;)
|
for (var i = 0; i < size; i++;)
|
||||||
@@ -147,7 +147,7 @@ obj vector<T> (Object) {
|
|||||||
return;
|
return;
|
||||||
data[index] = dataIn;
|
data[index] = dataIn;
|
||||||
}
|
}
|
||||||
fun add_all(dataIn: vector<T>): void {
|
fun add_all(dataIn: ref vector<T>): void {
|
||||||
for (var i = 0; i < dataIn.size; i++;)
|
for (var i = 0; i < dataIn.size; i++;)
|
||||||
addEnd(dataIn[i]);
|
addEnd(dataIn[i]);
|
||||||
}
|
}
|
||||||
@@ -156,8 +156,8 @@ obj vector<T> (Object) {
|
|||||||
if (!contains(dataIn))
|
if (!contains(dataIn))
|
||||||
addEnd(dataIn)
|
addEnd(dataIn)
|
||||||
}
|
}
|
||||||
fun add(dataIn: T): void { addEnd(dataIn); }
|
fun add(dataIn: ref T): void { addEnd(dataIn); }
|
||||||
fun addEnd(dataIn: T): void {
|
fun addEnd(dataIn: ref T): void {
|
||||||
if (size+1 >= available)
|
if (size+1 >= available)
|
||||||
resize((size+1)*2);
|
resize((size+1)*2);
|
||||||
maybe_copy_construct(&data[size], &dataIn);
|
maybe_copy_construct(&data[size], &dataIn);
|
||||||
@@ -173,6 +173,10 @@ obj vector<T> (Object) {
|
|||||||
size--
|
size--
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun for_each(func: fun(ref T):void):void {
|
||||||
|
for (var i = 0; i < size; i++;)
|
||||||
|
func(data[i])
|
||||||
|
}
|
||||||
fun for_each(func: fun(T):void):void {
|
fun for_each(func: fun(T):void):void {
|
||||||
for (var i = 0; i < size; i++;)
|
for (var i = 0; i < size; i++;)
|
||||||
func(data[i])
|
func(data[i])
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
# comment
|
# comment
|
||||||
a = b ;
|
Goal = a ;
|
||||||
|
a = b | rec ;
|
||||||
b = "c":named_c ;
|
b = "c":named_c ;
|
||||||
b = c "d":dname ;
|
b = c "d":dname ;
|
||||||
c = "a" | d ;
|
c = "a" | d ;
|
||||||
@@ -9,3 +10,4 @@ e = f | ;
|
|||||||
f = ;
|
f = ;
|
||||||
post_null = "hi" ;
|
post_null = "hi" ;
|
||||||
post_non_null = "bye" ;
|
post_non_null = "bye" ;
|
||||||
|
rec = "hmm" rec | ;
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ import symbol:*
|
|||||||
|
|
||||||
fun main():int {
|
fun main():int {
|
||||||
|
|
||||||
//var a = load_grammer(read_file(string("../krakenGrammer.kgm")))
|
var a = load_grammer(read_file(string("../krakenGrammer.kgm")))
|
||||||
/*var a = load_grammer(read_file(string("grammer.kgm")))*/
|
/*var a = load_grammer(read_file(string("grammer.kgm")))*/
|
||||||
var a = load_grammer(read_file(string("grammer2.kgm")))
|
//var a = load_grammer(read_file(string("grammer2.kgm")))
|
||||||
println(a.to_string())
|
println(a.to_string())
|
||||||
var doFirstSet = fun() {
|
var doFirstSet = fun() {
|
||||||
a.calculate_first_set()
|
a.calculate_first_set()
|
||||||
|
|||||||
@@ -16,3 +16,6 @@ true
|
|||||||
all:
|
all:
|
||||||
4
|
4
|
||||||
5
|
5
|
||||||
|
all ref:
|
||||||
|
4
|
||||||
|
5
|
||||||
|
|||||||
@@ -27,5 +27,7 @@ fun main():int {
|
|||||||
|
|
||||||
println("all:")
|
println("all:")
|
||||||
s.for_each( fun(it: int) println(it); )
|
s.for_each( fun(it: int) println(it); )
|
||||||
|
println("all ref:")
|
||||||
|
s.for_each( fun(it: ref int) println(it); )
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
15513
|
15513
|
||||||
3.7000007.7000007.70000015.700000
|
3.7000007.7000007.70000015.700000
|
||||||
123456789101112
|
123456789101112
|
||||||
|
with references
|
||||||
|
123456789101112
|
||||||
Constructed: 0
|
Constructed: 0
|
||||||
Copied: 0 to 1
|
Copied: 0 to 1
|
||||||
Copied: 1 to 2
|
Copied: 1 to 2
|
||||||
|
|||||||
@@ -69,6 +69,11 @@ fun main(): int {
|
|||||||
vector(1,2,3,4,5,6,7,8,9,10,11,12).for_each(fun(i:int) { print(i); })
|
vector(1,2,3,4,5,6,7,8,9,10,11,12).for_each(fun(i:int) { print(i); })
|
||||||
println()
|
println()
|
||||||
|
|
||||||
|
// with references
|
||||||
|
println("with references")
|
||||||
|
vector(1,2,3,4,5,6,7,8,9,10,11,12).for_each(fun(i: ref int) { print(i); })
|
||||||
|
println()
|
||||||
|
|
||||||
var desVec: *vector<AbleToBeDestroyed> = new<vector<AbleToBeDestroyed>>()->construct();
|
var desVec: *vector<AbleToBeDestroyed> = new<vector<AbleToBeDestroyed>>()->construct();
|
||||||
var testDestruct.construct(0): AbleToBeDestroyed;
|
var testDestruct.construct(0): AbleToBeDestroyed;
|
||||||
desVec->addEnd(testDestruct);
|
desVec->addEnd(testDestruct);
|
||||||
|
|||||||
Reference in New Issue
Block a user