Compare commits
	
		
			4 Commits
		
	
	
		
			2ae3086481
			...
			master
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| d85efb69dc | |||
| a6c3a0e39e | |||
| e4432cbe21 | |||
| 26cb591c49 | 
@@ -51,7 +51,7 @@ Value handleStringGet(const Value& subject, const std::vector<Value>& args) {
 | 
				
			|||||||
        error("String index out of bounds");
 | 
					        error("String index out of bounds");
 | 
				
			||||||
        return Value();
 | 
					        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) {
 | 
					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) {
 | 
					Value::Value(std::string stringval) {
 | 
				
			||||||
    // This constructor will attempt to parse the string into the most specific type possible.
 | 
					    // 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() == '"') {
 | 
					    if (stringval.length() >= 2 && stringval.front() == '"' && stringval.back() == '"') {
 | 
				
			||||||
        valtype = ValueType::String;
 | 
					        valtype = ValueType::String;
 | 
				
			||||||
        string_val = interpretEscapeSequences(stringval.substr(1, stringval.length() - 2));
 | 
					        string_val = interpretEscapeSequences(stringval.substr(1, stringval.length() - 2));
 | 
				
			||||||
        return;
 | 
					        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()) {
 | 
					    if (stringval.empty()) {
 | 
				
			||||||
        valtype = ValueType::None;
 | 
					        valtype = ValueType::None;
 | 
				
			||||||
        string_val = "";
 | 
					        string_val = "";
 | 
				
			||||||
@@ -274,7 +274,7 @@ std::vector<Value> split(std::string line) {
 | 
				
			|||||||
                splitvals.push_back(Value(buf));
 | 
					                splitvals.push_back(Value(buf));
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            buf = "";
 | 
					            buf = "";
 | 
				
			||||||
        } else if (chr == '(' || chr == '[') {
 | 
					        } else if ((chr == '(' || chr == '[') && !instring) {
 | 
				
			||||||
            if (brackets == 0) {
 | 
					            if (brackets == 0) {
 | 
				
			||||||
                bracket_type = chr;
 | 
					                bracket_type = chr;
 | 
				
			||||||
                if (!buf.empty()) {
 | 
					                if (!buf.empty()) {
 | 
				
			||||||
@@ -285,7 +285,7 @@ std::vector<Value> split(std::string line) {
 | 
				
			|||||||
                buf += chr;
 | 
					                buf += chr;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            brackets++;
 | 
					            brackets++;
 | 
				
			||||||
        } else if (chr == ')' || chr == ']') {
 | 
					        } else if ((chr == ')' || chr == ']') && !instring) {
 | 
				
			||||||
            brackets--;
 | 
					            brackets--;
 | 
				
			||||||
            if (brackets == 0) {
 | 
					            if (brackets == 0) {
 | 
				
			||||||
                if (!buf.empty()) {
 | 
					                if (!buf.empty()) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -67,6 +67,7 @@ struct Instruction {
 | 
				
			|||||||
    InstructionType instruction = InstructionType::None;
 | 
					    InstructionType instruction = InstructionType::None;
 | 
				
			||||||
    std::vector<Value> args;
 | 
					    std::vector<Value> args;
 | 
				
			||||||
    std::string toString() const;
 | 
					    std::string toString() const;
 | 
				
			||||||
 | 
					    int lineNum;
 | 
				
			||||||
    Instruction() = default;
 | 
					    Instruction() = default;
 | 
				
			||||||
    Instruction(std::vector<std::string> toks);
 | 
					    Instruction(std::vector<std::string> toks);
 | 
				
			||||||
    Instruction(std::vector<Value> toks);
 | 
					    Instruction(std::vector<Value> toks);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,6 +7,7 @@ Value modules::concat(std::vector<Value> args) {
 | 
				
			|||||||
    std::string buf;
 | 
					    std::string buf;
 | 
				
			||||||
    for (Value val : args) {
 | 
					    for (Value val : args) {
 | 
				
			||||||
        switch (val.valtype) {
 | 
					        switch (val.valtype) {
 | 
				
			||||||
 | 
					            case ValueType::Identifier:
 | 
				
			||||||
            case ValueType::String:
 | 
					            case ValueType::String:
 | 
				
			||||||
                buf += val.string_val;
 | 
					                buf += val.string_val;
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
@@ -17,7 +18,7 @@ Value modules::concat(std::vector<Value> args) {
 | 
				
			|||||||
                buf += std::to_string(val.double_val);
 | 
					                buf += std::to_string(val.double_val);
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            default:
 | 
					            default:
 | 
				
			||||||
                error("Can only concatenate String, Int, and Double values");
 | 
					                error("Can only concatenate String, Int, and Double values. Value given: " + val.toString());
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,8 +1,45 @@
 | 
				
			|||||||
#include "split.h"
 | 
					#include "split.h"
 | 
				
			||||||
#include "../../defs/defs.h"
 | 
					#include "../../defs/defs.h"
 | 
				
			||||||
 | 
					#include "../../error/error.h"
 | 
				
			||||||
#include <vector>
 | 
					#include <vector>
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Value modules::split(std::vector<Value> args) {
 | 
					Value modules::split(std::vector<Value> args) {
 | 
				
			||||||
    return Value("Work in progress!");
 | 
					    if (args.size() < 1) {
 | 
				
			||||||
 | 
					        error("Not enough args for split module");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (args[0].valtype != ValueType::String) {
 | 
				
			||||||
 | 
					        error("First argument of split must be a string");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    std::string delimiter = " ";
 | 
				
			||||||
 | 
					    if (args.size() > 1) {
 | 
				
			||||||
 | 
					        if (args[1].valtype == ValueType::String) {
 | 
				
			||||||
 | 
					            delimiter = args[1].string_val;
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            error("Expected a string delimiter as second argument of split");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    std::string buf;
 | 
				
			||||||
 | 
					    std::vector<Value> list;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (const char& chr : args[0].string_val) {
 | 
				
			||||||
 | 
					        buf += chr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (buf.size() >= delimiter.size()) {
 | 
				
			||||||
 | 
					            std::string checker = buf.substr(buf.size() - delimiter.size());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (checker == delimiter) {
 | 
				
			||||||
 | 
					                list.push_back(Value("\"" + buf.substr(0, buf.size() - delimiter.size()) + "\""));
 | 
				
			||||||
 | 
					                buf.clear();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (!buf.empty()) {
 | 
				
			||||||
 | 
					        list.push_back(Value("\"" + buf + "\""));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return Value(list);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,8 +9,10 @@ std::vector<Instruction> parse(std::string program) {
 | 
				
			|||||||
    std::vector<std::string> lines;
 | 
					    std::vector<std::string> lines;
 | 
				
			||||||
    std::string buf;
 | 
					    std::string buf;
 | 
				
			||||||
    int blockTracker = 0;
 | 
					    int blockTracker = 0;
 | 
				
			||||||
 | 
					    int lineNum = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (char chr : program) {
 | 
					    for (char chr : program) {
 | 
				
			||||||
 | 
					        if (chr == '\n') lineNum ++;
 | 
				
			||||||
        if (chr == '{') {
 | 
					        if (chr == '{') {
 | 
				
			||||||
            blockTracker++;
 | 
					            blockTracker++;
 | 
				
			||||||
        } else if (chr == '}') {
 | 
					        } else if (chr == '}') {
 | 
				
			||||||
@@ -23,7 +25,7 @@ std::vector<Instruction> parse(std::string program) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        if ((chr == '\n' || chr == ';') && blockTracker == 0) {
 | 
					        if ((chr == '\n' || chr == ';') && blockTracker == 0) {
 | 
				
			||||||
            if (!buf.empty()) {
 | 
					            if (!buf.empty()) {
 | 
				
			||||||
                lines.push_back(buf);
 | 
					                lines.push_back(trim(buf));
 | 
				
			||||||
                buf.clear();
 | 
					                buf.clear();
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
@@ -90,9 +92,18 @@ std::vector<Instruction> parse(std::string program) {
 | 
				
			|||||||
                    error("Expected '{' for else statement");
 | 
					                    error("Expected '{' for else statement");
 | 
				
			||||||
                    continue;
 | 
					                    continue;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                size_t else_block_end = remaining_line.find_last_of('}');
 | 
					                size_t else_block_end = 0;
 | 
				
			||||||
                 if (else_block_end == std::string::npos) {
 | 
					                int brace_level = 0;
 | 
				
			||||||
                    error("Expected '}' for else statement");
 | 
					                for (size_t i = else_block_start; i < remaining_line.length(); ++i) {
 | 
				
			||||||
 | 
					                    if (remaining_line[i] == '{') brace_level++;
 | 
				
			||||||
 | 
					                    else if (remaining_line[i] == '}') brace_level--;
 | 
				
			||||||
 | 
					                    if (brace_level == 0) {
 | 
				
			||||||
 | 
					                        else_block_end = i;
 | 
				
			||||||
 | 
					                        break;
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                 if (else_block_end == 0) {
 | 
				
			||||||
 | 
					                    error("Mismatched braces in else statement");
 | 
				
			||||||
                    continue;
 | 
					                    continue;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                std::string else_content = remaining_line.substr(else_block_start + 1, else_block_end - else_block_start - 1);
 | 
					                std::string else_content = remaining_line.substr(else_block_start + 1, else_block_end - else_block_start - 1);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,11 +2,11 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Helper to trim whitespace from the start and end of a string
 | 
					// Helper to trim whitespace from the start and end of a string
 | 
				
			||||||
std::string trim(const std::string& str) {
 | 
					std::string trim(const std::string& str) {
 | 
				
			||||||
    size_t first = str.find_first_not_of(" \t\n\r");
 | 
					    size_t first = str.find_first_not_of(" \t\n");
 | 
				
			||||||
    if (std::string::npos == first) {
 | 
					    if (std::string::npos == first) {
 | 
				
			||||||
        return str;
 | 
					        return "";
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    size_t last = str.find_last_not_of(" \t\n\r");
 | 
					    size_t last = str.find_last_not_of(" \t\n");
 | 
				
			||||||
    return str.substr(first, (last - first + 1));
 | 
					    return str.substr(first, (last - first + 1));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										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