minor fixes and toInt.kyn
This commit is contained in:
@@ -51,7 +51,7 @@ Value handleStringGet(const Value& subject, const std::vector<Value>& args) {
|
||||
error("String index out of bounds");
|
||||
return Value();
|
||||
}
|
||||
return Value(std::string(1, subject.string_val.at(index)));
|
||||
return Value("\"" + std::string(1, subject.string_val.at(index)) + "\"");
|
||||
}
|
||||
|
||||
void handleStringSet(std::string var_name, Value& subject, const Value& index_val, const Value& new_val) {
|
||||
|
||||
@@ -77,18 +77,18 @@ Instruction::Instruction(std::vector<Value> toks) {
|
||||
|
||||
Value::Value(std::string stringval) {
|
||||
// This constructor will attempt to parse the string into the most specific type possible.
|
||||
if (stringval.length() > 2 && stringval.front() == '<' && stringval.back() == '>') {
|
||||
valtype = ValueType::TypePlaceholder;
|
||||
type_placeholder_name = stringval.substr(1, stringval.length() - 2);
|
||||
return;
|
||||
}
|
||||
|
||||
if (stringval.length() >= 2 && stringval.front() == '"' && stringval.back() == '"') {
|
||||
valtype = ValueType::String;
|
||||
string_val = interpretEscapeSequences(stringval.substr(1, stringval.length() - 2));
|
||||
return;
|
||||
}
|
||||
|
||||
if (stringval.length() > 2 && stringval.front() == '<' && stringval.back() == '>') {
|
||||
valtype = ValueType::TypePlaceholder;
|
||||
type_placeholder_name = stringval.substr(1, stringval.length() - 2);
|
||||
return;
|
||||
}
|
||||
|
||||
if (stringval.empty()) {
|
||||
valtype = ValueType::None;
|
||||
string_val = "";
|
||||
@@ -274,7 +274,7 @@ std::vector<Value> split(std::string line) {
|
||||
splitvals.push_back(Value(buf));
|
||||
}
|
||||
buf = "";
|
||||
} else if (chr == '(' || chr == '[') {
|
||||
} else if ((chr == '(' || chr == '[') && !instring) {
|
||||
if (brackets == 0) {
|
||||
bracket_type = chr;
|
||||
if (!buf.empty()) {
|
||||
@@ -285,7 +285,7 @@ std::vector<Value> split(std::string line) {
|
||||
buf += chr;
|
||||
}
|
||||
brackets++;
|
||||
} else if (chr == ')' || chr == ']') {
|
||||
} else if ((chr == ')' || chr == ']') && !instring) {
|
||||
brackets--;
|
||||
if (brackets == 0) {
|
||||
if (!buf.empty()) {
|
||||
|
||||
@@ -67,6 +67,7 @@ struct Instruction {
|
||||
InstructionType instruction = InstructionType::None;
|
||||
std::vector<Value> args;
|
||||
std::string toString() const;
|
||||
int lineNum;
|
||||
Instruction() = default;
|
||||
Instruction(std::vector<std::string> toks);
|
||||
Instruction(std::vector<Value> toks);
|
||||
|
||||
@@ -7,6 +7,7 @@ Value modules::concat(std::vector<Value> args) {
|
||||
std::string buf;
|
||||
for (Value val : args) {
|
||||
switch (val.valtype) {
|
||||
case ValueType::Identifier:
|
||||
case ValueType::String:
|
||||
buf += val.string_val;
|
||||
break;
|
||||
@@ -17,7 +18,7 @@ Value modules::concat(std::vector<Value> args) {
|
||||
buf += std::to_string(val.double_val);
|
||||
break;
|
||||
default:
|
||||
error("Can only concatenate String, Int, and Double values");
|
||||
error("Can only concatenate String, Int, and Double values. Value given: " + val.toString());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,10 @@ std::vector<Instruction> parse(std::string program) {
|
||||
std::vector<std::string> lines;
|
||||
std::string buf;
|
||||
int blockTracker = 0;
|
||||
int lineNum = 0;
|
||||
|
||||
for (char chr : program) {
|
||||
if (chr == '\n') lineNum ++;
|
||||
if (chr == '{') {
|
||||
blockTracker++;
|
||||
} else if (chr == '}') {
|
||||
@@ -23,7 +25,7 @@ std::vector<Instruction> parse(std::string program) {
|
||||
|
||||
if ((chr == '\n' || chr == ';') && blockTracker == 0) {
|
||||
if (!buf.empty()) {
|
||||
lines.push_back(buf);
|
||||
lines.push_back(trim(buf));
|
||||
buf.clear();
|
||||
}
|
||||
} else {
|
||||
|
||||
47
tests/toInt.kyn
Normal file
47
tests/toInt.kyn
Normal file
@@ -0,0 +1,47 @@
|
||||
fun toInt in {
|
||||
assert $in is <String>
|
||||
let retint = 0
|
||||
let counter = 0
|
||||
let power = ($in size)
|
||||
let size = ($in size)
|
||||
while compare $counter < $size {
|
||||
let char = ($in $counter)
|
||||
power = (math $power - 1)
|
||||
if compare $char == "1" {
|
||||
retint = (math $retint + (math 1 * 10 ^ $power))
|
||||
}
|
||||
if compare $char == "2" {
|
||||
retint = (math $retint + (math 2 * 10 ^ $power))
|
||||
}
|
||||
if compare $char == "3" {
|
||||
retint = (math $retint + (math 3 * 10 ^ $power))
|
||||
}
|
||||
if compare $char == "4" {
|
||||
retint = (math $retint + (math 4 * 10 ^ $power))
|
||||
}
|
||||
if compare $char == "5" {
|
||||
retint = (math $retint + (math 5 * 10 ^ $power))
|
||||
}
|
||||
if compare $char == "6" {
|
||||
retint = (math $retint + (math 6 * 10 ^ $power))
|
||||
}
|
||||
if compare $char == "7" {
|
||||
retint = (math $retint + (math 7 * 10 ^ $power))
|
||||
}
|
||||
if compare $char == "8" {
|
||||
retint = (math $retint + (math 8 * 10 ^ $power))
|
||||
}
|
||||
if compare $char == "9" {
|
||||
retint = (math $retint + (math 9 * 10 ^ $power))
|
||||
}
|
||||
if compare $char == "0" {
|
||||
# whole lotta nothing
|
||||
}
|
||||
counter = (math $counter + 1)
|
||||
|
||||
}
|
||||
return $retint
|
||||
}
|
||||
let myInt = (toInt "4738927643289")
|
||||
|
||||
println $myInt
|
||||
Reference in New Issue
Block a user