Some bugfixes, allow overloading of [] and add that to vector and string, work on regex. Need closures before that finishes....
This commit is contained in:
@@ -9,18 +9,14 @@
|
||||
|
||||
class RegExState {
|
||||
public:
|
||||
RegExState(RegExState* inInnerState);
|
||||
RegExState(char inCharacter);
|
||||
RegExState();
|
||||
|
||||
~RegExState();
|
||||
|
||||
void addNext(RegExState* nextState);
|
||||
bool characterIs(char inCharacter);
|
||||
std::vector<RegExState*>* advance(char advanceCharacter);
|
||||
std::vector<RegExState*>* getNextStates();
|
||||
|
||||
RegExState* getInner();
|
||||
std::vector<RegExState*> getNextStates();
|
||||
|
||||
bool isGoal();
|
||||
std::string toString();
|
||||
@@ -31,7 +27,6 @@ class RegExState {
|
||||
|
||||
private:
|
||||
std::vector<RegExState*> nextStates;
|
||||
RegExState* inner;
|
||||
char character;
|
||||
};
|
||||
#endif
|
||||
@@ -61,7 +61,7 @@ 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
|
||||
right_shift = ">" ">" ;
|
||||
overloadable_operator = "\+" | "-" | "\*" | "/" | "%" | "^" | "&" | "\|" | "~" | "!" | "," | "=" | "\+\+" | "--" | "<<" | right_shift | "==" | "!=" | "&&" | "\|\|" | "\+=" | "-=" | "/=" | "%=" | "^=" | "&=" | "\|=" | "\*=" | "<<=" | ">>=" | "->" | "\(" "\)" ;
|
||||
overloadable_operator = "\+" | "-" | "\*" | "/" | "%" | "^" | "&" | "\|" | "~" | "!" | "," | "=" | "\+\+" | "--" | "<<" | right_shift | "==" | "!=" | "&&" | "\|\|" | "\+=" | "-=" | "/=" | "%=" | "^=" | "&=" | "\|=" | "\*=" | "<<=" | ">>=" | "->" | "\(" "\)" | "[]" ;
|
||||
func_identifier = identifier | identifier overloadable_operator ;
|
||||
function = "fun" WS func_identifier WS template_dec WS "\(" WS opt_typed_parameter_list WS "\)" WS dec_type WS statement | "fun" WS func_identifier WS "\(" WS opt_typed_parameter_list WS "\)" WS dec_type WS statement ;
|
||||
lambda = "fun" WS "\(" WS opt_typed_parameter_list WS "\)" WS dec_type WS statement ;
|
||||
|
||||
@@ -972,7 +972,8 @@ NodeTree<ASTData>* ASTTransformation::templateClassLookup(NodeTree<ASTData>* sco
|
||||
int typeIndex = 0;
|
||||
int currentTraitsSatisfied = 0;
|
||||
for (auto j : nameTraitsPairs) {
|
||||
if (!subset(j.second, templateInstantiationTypes[typeIndex]->traits)) {
|
||||
// error out if not subset, or if we're a pointer but should have traits
|
||||
if (!subset(j.second, templateInstantiationTypes[typeIndex]->traits) || (templateInstantiationTypes[typeIndex]->getIndirection() && j.second.size())) {
|
||||
traitsEqual = false;
|
||||
std::cout << "Traits not subset for " << j.first << " and " << templateInstantiationTypes[typeIndex]->toString() << ": ";
|
||||
//std::cout << baseType << " " << indirection << " " << typeDefinition << " " << templateDefinition << " " << traits << ;
|
||||
@@ -1137,7 +1138,8 @@ NodeTree<ASTData>* ASTTransformation::templateFunctionLookup(NodeTree<ASTData>*
|
||||
int typeIndex = 0;
|
||||
int currentTraitsSatisfied = 0;
|
||||
for (auto j : nameTraitsPairs) {
|
||||
if (!subset(j.second, templateInstantiationTypesPerFunction[i][typeIndex]->traits)) {
|
||||
// error out if not subset, or if we're a pointer but should have traits
|
||||
if (!subset(j.second, templateInstantiationTypesPerFunction[i][typeIndex]->traits) || (templateInstantiationTypesPerFunction[i][typeIndex]->getIndirection() && j.second.size())) {
|
||||
traitsEqual = false;
|
||||
std::cout << "Traits not a subset for " << j.first << " and " << templateInstantiationTypesPerFunction[i][typeIndex]->toString() << ": |";
|
||||
std::copy(j.second.begin(), j.second.end(), std::ostream_iterator<std::string>(std::cout, " "));
|
||||
|
||||
@@ -359,7 +359,7 @@ CCodeTriple CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
|
||||
return tabs() + stat.preValue + stat.value + ";\n" + stat.postValue ;
|
||||
}
|
||||
case if_statement:
|
||||
output += "if (" + generate(children[0], enclosingObject, true).oneString() + ")\n\t";
|
||||
output += "if (" + generate(children[0], enclosingObject, true) + ")\n\t";
|
||||
// We have to see if the then statement is a regular single statement or a block.
|
||||
// If it's a block, because it's also a statement a semicolon will be emitted even though
|
||||
// we don't want it to be, as if (a) {b}; else {c}; is not legal C, but if (a) {b} else {c}; is.
|
||||
@@ -798,16 +798,16 @@ std::string CGenerator::CifyName(std::string name) {
|
||||
"&", "amprsd",
|
||||
"|", "pipe",
|
||||
"~", "tilde",
|
||||
"!", "exclamationpt",
|
||||
"!", "exlmtnpt",
|
||||
",", "comma",
|
||||
"=", "equals",
|
||||
"++", "doubleplus",
|
||||
"--", "doubleminus",
|
||||
"<<", "doubleleft",
|
||||
">>", "doubleright",
|
||||
"=", "eq",
|
||||
"++", "dbplus",
|
||||
"--", "dbminus",
|
||||
"<<", "dbleft",
|
||||
">>", "dbright",
|
||||
"::", "scopeop",
|
||||
":", "colon",
|
||||
"==", "doubleequals",
|
||||
"==", "dbq",
|
||||
"!=", "notequals",
|
||||
"&&", "doubleamprsnd",
|
||||
"||", "doublepipe",
|
||||
@@ -820,13 +820,13 @@ std::string CGenerator::CifyName(std::string name) {
|
||||
"|=", "pipeequals",
|
||||
"*=", "starequals",
|
||||
"<<=", "doublerightequals",
|
||||
"<", "lessthan",
|
||||
">", "greaterthan",
|
||||
"<", "lt",
|
||||
">", "gt",
|
||||
">>=", "doubleleftequals",
|
||||
"(", "openparen",
|
||||
")", "closeparen",
|
||||
"[", "openbracket",
|
||||
"]", "closebracket",
|
||||
"[", "obk",
|
||||
"]", "cbk",
|
||||
" ", "space",
|
||||
".", "dot",
|
||||
"->", "arrow" };
|
||||
|
||||
@@ -70,7 +70,7 @@ RegExState* RegEx::construct(std::vector<RegExState*>* ending, std::string patte
|
||||
int perenEnd = findPerenEnd(pattern, i);
|
||||
RegExState* innerBegin = construct(&innerEnds, strSlice(pattern, i+1, perenEnd));
|
||||
i = perenEnd;
|
||||
std::vector<RegExState*> innerBegins = *(innerBegin->getNextStates());
|
||||
std::vector<RegExState*> innerBegins = innerBegin->getNextStates();
|
||||
if (alternating) {
|
||||
for (std::vector<RegExState*>::size_type j = 0; j < previousStatesEnd.size(); j++)
|
||||
for (std::vector<RegExState*>::size_type k = 0; k < innerBegins.size(); k++)
|
||||
|
||||
@@ -1,17 +1,11 @@
|
||||
#include "RegExState.h"
|
||||
|
||||
RegExState::RegExState(RegExState* inInnerState) {
|
||||
inner = inInnerState;
|
||||
}
|
||||
|
||||
RegExState::RegExState(char inCharacter) {
|
||||
character = inCharacter;
|
||||
inner = NULL;
|
||||
}
|
||||
|
||||
RegExState::RegExState() {
|
||||
character = 0;
|
||||
inner = NULL;
|
||||
}
|
||||
|
||||
RegExState::~RegExState() {
|
||||
@@ -35,16 +29,11 @@ std::vector<RegExState*>* RegExState::advance(char advanceCharacter) {
|
||||
return advanceStates;
|
||||
}
|
||||
|
||||
RegExState* RegExState::getInner() {
|
||||
return inner;
|
||||
}
|
||||
|
||||
std::vector<RegExState*>* RegExState::getNextStates() {
|
||||
return &nextStates;
|
||||
std::vector<RegExState*> RegExState::getNextStates() {
|
||||
return nextStates;
|
||||
}
|
||||
|
||||
bool RegExState::isGoal() {
|
||||
//return inner == NULL && nextStates.size() == 0;
|
||||
for (std::vector<RegExState*>::size_type i = 0; i < nextStates.size(); i++)
|
||||
if (nextStates[i] == NULL)
|
||||
return true;
|
||||
@@ -66,11 +55,6 @@ std::string RegExState::toString(std::vector<RegExState*>* avoid) {
|
||||
avoid->push_back(this);
|
||||
std::string string = "";
|
||||
string += std::string("\"") + character + "\"";
|
||||
if (inner != NULL) {
|
||||
string += "inner: ";
|
||||
string += inner->toString(avoid);
|
||||
string += " end inner ";
|
||||
}
|
||||
for (std::vector<RegExState*>::size_type i = 0; i < nextStates.size(); i++) {
|
||||
bool inAvoid = false;
|
||||
for (std::vector<RegExState*>::size_type j = 0; j < avoid->size(); j++) {
|
||||
@@ -90,7 +74,6 @@ std::string RegExState::toString(std::vector<RegExState*>* avoid) {
|
||||
else
|
||||
string += "->this";
|
||||
}
|
||||
//std::cout << "inner = " << inner << " nextStates size = " << nextStates.size() <<std::endl;
|
||||
return string;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,16 @@ fun println<T>(toPrint: T) : void {
|
||||
fun print(toPrint: char*) : void {
|
||||
__if_comp__ __C__ {
|
||||
simple_passthrough(toPrint = toPrint::) """
|
||||
printf(toPrint);
|
||||
printf("%s", toPrint);
|
||||
"""
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
fun print(toPrint: char) : void {
|
||||
__if_comp__ __C__ {
|
||||
simple_passthrough(toPrint = toPrint::) """
|
||||
printf("%c", toPrint);
|
||||
"""
|
||||
}
|
||||
return;
|
||||
|
||||
78
stdlib/regex.krak
Normal file
78
stdlib/regex.krak
Normal file
@@ -0,0 +1,78 @@
|
||||
import io
|
||||
import vector
|
||||
import string
|
||||
|
||||
fun regex(in: char*):regex {
|
||||
return regex(string::string(in))
|
||||
}
|
||||
fun regex(in: string::string):regex {
|
||||
var out.construct(in):regex
|
||||
return out
|
||||
}
|
||||
|
||||
obj regexState(Object) {
|
||||
var character: char
|
||||
var next_states: vector::vector<regexState*>
|
||||
fun construct(charIn:char): regexState* {
|
||||
character = charIn
|
||||
next_states.construct()
|
||||
return this
|
||||
}
|
||||
fun construct(): regexState* {
|
||||
return construct(0)
|
||||
}
|
||||
fun copy_construct(old:regexState*): void {
|
||||
character = regexState->character
|
||||
next_states.copy_construct(®exState->next_states)
|
||||
}
|
||||
fun destruct():void {
|
||||
next_states.destruct()
|
||||
}
|
||||
fun match(input: char): vector::vector<regexState*> {
|
||||
return next_states.filter(fun(it:regexState*):bool { return it->character == input; } )
|
||||
}
|
||||
}
|
||||
|
||||
obj regex(Object) {
|
||||
var regexString: string::string
|
||||
var begin: regexState
|
||||
fun construct(regexStringIn: string::string): regex* {
|
||||
regexState.construct()
|
||||
regexString.copy_construct(®exStringIn)
|
||||
|
||||
var traverse = &begin
|
||||
for (var i = 0; i < regexString.length(); i++;) {
|
||||
var next = new<regexState>()->construct(regexString[i])
|
||||
traverse->next_states->add(next)
|
||||
traverse = next
|
||||
}
|
||||
traverse->next_states->add(new<regexState>()->construct(1))
|
||||
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old:regex*):void {
|
||||
begin.copy_construct(&old->begin)
|
||||
regexString.copy_construct(&old->regexString)
|
||||
}
|
||||
fun destruct():void {
|
||||
begin.destruct()
|
||||
regexString.destruct()
|
||||
}
|
||||
fun long_match(to_match: char*): int { return long_match(string::string(to_match)); }
|
||||
fun long_match(to_match: string::string): int {
|
||||
var next = vector::vector(&begin)
|
||||
var longest = 0
|
||||
for (var i = 0; i < to_match.length(); i++;) {
|
||||
if (next.size == 0)
|
||||
return longest
|
||||
if (next.any_true(fun(state: regexState*):bool { return state->character == 1; }))
|
||||
longest = i
|
||||
next = next.flatten_map(fun(state: regexState*): vector::vector<regexState*> { return state->match(to_match[i]); })
|
||||
}
|
||||
if (next.any_true(fun(state: regexState*):bool { return state->character == 1; }))
|
||||
return to_match.length()
|
||||
return longest
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import vector;
|
||||
import mem;
|
||||
|
||||
fun string(in:char*):string {
|
||||
var out:string = in
|
||||
return out
|
||||
}
|
||||
|
||||
obj string (Object) {
|
||||
var data: vector::vector<char>;
|
||||
fun construct(): string* {
|
||||
@@ -33,6 +38,9 @@ obj string (Object) {
|
||||
data.destruct()
|
||||
}
|
||||
|
||||
fun operator[](index: int): char { return data[index]; }
|
||||
fun length():int { return data.size; }
|
||||
|
||||
fun operator=(str: char*): void {
|
||||
destruct();
|
||||
construct(str)
|
||||
|
||||
@@ -74,10 +74,8 @@ obj vector<T> (Object) {
|
||||
return true;
|
||||
}
|
||||
|
||||
fun at(index: int): T {
|
||||
return get(index);
|
||||
}
|
||||
|
||||
fun at(index: int): T { return get(index); }
|
||||
fun operator[](index: int): T { return get(index); }
|
||||
fun get(index: int): T {
|
||||
if (index < 0 || index >= size) {
|
||||
println("Vector access out of bounds! Retuning 0th element as sanest option");
|
||||
@@ -97,6 +95,7 @@ obj vector<T> (Object) {
|
||||
return;
|
||||
data[index] = dataIn;
|
||||
}
|
||||
fun add(dataIn: T): void { addEnd(dataIn); }
|
||||
fun addEnd(dataIn: T): void {
|
||||
size++;
|
||||
if (size >= available)
|
||||
@@ -117,5 +116,20 @@ obj vector<T> (Object) {
|
||||
newVec.addEnd(func(data[i]))
|
||||
return newVec
|
||||
}
|
||||
fun flatten_map<U>(func: fun(T):vector<U>):vector<U> {
|
||||
var newVec.construct(): vector<U>
|
||||
for (var i = 0; i < size; i++;) {
|
||||
var to_add = func(data[i])
|
||||
for (var j = 0; j < to_add.size; j++;)
|
||||
newVec.addEnd(to_add.get(j))
|
||||
}
|
||||
return newVec
|
||||
}
|
||||
fun any_true(func: fun(T):bool):bool {
|
||||
for (var i = 0; i < size; i++;)
|
||||
if (func(data[i]))
|
||||
return true
|
||||
return false
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
1
tests/test_regex.expected_results
Normal file
1
tests/test_regex.expected_results
Normal file
@@ -0,0 +1 @@
|
||||
a
|
||||
9
tests/test_regex.krak
Normal file
9
tests/test_regex.krak
Normal file
@@ -0,0 +1,9 @@
|
||||
import io:*
|
||||
import regex:*
|
||||
|
||||
fun main():int {
|
||||
var reg = regex("a")
|
||||
println(reg.regexString)
|
||||
println(reg.long_match("a"))
|
||||
return 0
|
||||
}
|
||||
0
tests/test_regex/test_regex.results
Normal file
0
tests/test_regex/test_regex.results
Normal file
@@ -4,3 +4,5 @@ assignment overload2
|
||||
assignment overload with additional
|
||||
hope
|
||||
hope3
|
||||
new way!
|
||||
a
|
||||
|
||||
@@ -13,6 +13,9 @@ fun main(): int {
|
||||
var initilized:string::string = "hope"
|
||||
io::println(initilized)
|
||||
io::println(initilized+ "3")
|
||||
var newWay = string::string("new way!")
|
||||
io::println(newWay)
|
||||
io::println(newWay[5])
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,22 @@ No Traits
|
||||
First Trait
|
||||
Second Trait
|
||||
Both Traits
|
||||
No Traits
|
||||
|
||||
No Traits
|
||||
First Trait
|
||||
Second Trait
|
||||
Both Traits
|
||||
No Traits
|
||||
|
||||
First Trait
|
||||
Second Trait
|
||||
Both Traits
|
||||
No Traits
|
||||
First Trait
|
||||
|
||||
First Trait
|
||||
Second Trait
|
||||
Both Traits
|
||||
No Traits
|
||||
First Trait
|
||||
|
||||
@@ -48,13 +48,22 @@ fun main(): int {
|
||||
var c: Trait2;
|
||||
var d: TwoTrait;
|
||||
var e: AlreadySpecilized;
|
||||
var f: TwoTrait*;
|
||||
|
||||
OneTwoFunc<NoTraits>(a);
|
||||
OneTwoFunc<Trait1>(b);
|
||||
OneTwoFunc<Trait2>(c);
|
||||
OneTwoFunc<TwoTrait>(d);
|
||||
// OneTwoFunc<AlreadySpecilized>(e);
|
||||
OneTwoFunc<TwoTrait*>(f);
|
||||
println();
|
||||
|
||||
OneTwoFunc(a);
|
||||
OneTwoFunc(b);
|
||||
OneTwoFunc(c);
|
||||
OneTwoFunc(d);
|
||||
// OneTwoFunc(e);
|
||||
OneTwoFunc(f);
|
||||
println();
|
||||
|
||||
var alpha: OneTwoObj<NoTraits>;
|
||||
@@ -62,11 +71,20 @@ fun main(): int {
|
||||
var gamma: OneTwoObj<Trait2>;
|
||||
var delta: OneTwoObj<TwoTrait>;
|
||||
// |OneTwoObj<AlreadySpecilized>| epsilon;
|
||||
var zeta: OneTwoObj<TwoTrait*>;
|
||||
|
||||
OneTwoFunc<OneTwoObj<NoTraits>>(alpha);
|
||||
OneTwoFunc<OneTwoObj<Trait1>>(beta);
|
||||
OneTwoFunc<OneTwoObj<Trait2>>(gamma);
|
||||
OneTwoFunc<OneTwoObj<TwoTrait>>(delta);
|
||||
OneTwoFunc<OneTwoObj<TwoTrait*>>(zeta);
|
||||
println()
|
||||
|
||||
OneTwoFunc(alpha);
|
||||
OneTwoFunc(beta);
|
||||
OneTwoFunc(gamma);
|
||||
OneTwoFunc(delta);
|
||||
OneTwoFunc(zeta);
|
||||
|
||||
//We can't pass along our inner part, so let's just make sure that it is the right object.
|
||||
//epsilon.proveSpecilized();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
4
|
||||
1337
|
||||
1337
|
||||
26614
|
||||
9131321
|
||||
15513
|
||||
|
||||
@@ -43,6 +43,9 @@ fun main(): int {
|
||||
for (var i: int = 0; i < intVec.size; i++;)
|
||||
print(intVec.at(i));
|
||||
println();
|
||||
for (var i: int = 0; i < intVec.size; i++;)
|
||||
print(intVec[i]);
|
||||
println();
|
||||
|
||||
// in place lambda map
|
||||
intVec.in_place(fun(it:int):int { return it*2; })
|
||||
|
||||
Reference in New Issue
Block a user