Compare commits
2 Commits
b628c00f9e
...
faebaf7d3f
| Author | SHA1 | Date | |
|---|---|---|---|
| faebaf7d3f | |||
| e3b24b585b |
1
.vim/ftdetect/kyn.vim
Normal file
1
.vim/ftdetect/kyn.vim
Normal file
@@ -0,0 +1 @@
|
|||||||
|
autocmd BufNewFile,BufRead *.kyn setfiletype kyn
|
||||||
47
.vim/syntax/kyn.vim
Normal file
47
.vim/syntax/kyn.vim
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
" Vim syntax file
|
||||||
|
" Language: Kyn
|
||||||
|
|
||||||
|
if exists("b:current_syntax")
|
||||||
|
finish
|
||||||
|
endif
|
||||||
|
|
||||||
|
" Keywords
|
||||||
|
syn keyword kynKeyword fun struct let if while return assert is
|
||||||
|
|
||||||
|
" Instructions/Modules
|
||||||
|
syn keyword kynInstruction println print math exit compare input concat split file
|
||||||
|
|
||||||
|
" Comments
|
||||||
|
syn match kynComment /#.*/
|
||||||
|
|
||||||
|
" Strings
|
||||||
|
syn region kynString start=/"/ end=/"/
|
||||||
|
|
||||||
|
" Numbers
|
||||||
|
syn match kynNumber /\d\+\(\.\d\+\)\?/
|
||||||
|
|
||||||
|
" Variables
|
||||||
|
syn match kynVariable /\$[a-zA-Z_][a-zA-Z0-9_]*/
|
||||||
|
|
||||||
|
" Type Placeholders
|
||||||
|
syn match kynType /<[a-zA-Z_][a-zA-Z0-9_]*>/
|
||||||
|
|
||||||
|
" Operators
|
||||||
|
syn match kynOperator /[=+\-*\/^%]/
|
||||||
|
syn match kynOperator /==\|!=\|>=\|<=/
|
||||||
|
|
||||||
|
" Special variables
|
||||||
|
syn keyword kynSpecial self
|
||||||
|
|
||||||
|
" Highlighting links
|
||||||
|
hi def link kynKeyword Keyword
|
||||||
|
hi def link kynInstruction Statement
|
||||||
|
hi def link kynComment Comment
|
||||||
|
hi def link kynString String
|
||||||
|
hi def link kynNumber Number
|
||||||
|
hi def link kynVariable Identifier
|
||||||
|
hi def link kynType Type
|
||||||
|
hi def link kynOperator Operator
|
||||||
|
hi def link kynSpecial Special
|
||||||
|
|
||||||
|
let b:current_syntax = "kyn"
|
||||||
3
Makefile
3
Makefile
@@ -27,8 +27,9 @@ TARGET = kyn
|
|||||||
all: $(TARGET)
|
all: $(TARGET)
|
||||||
|
|
||||||
# Link the object files to create the executable
|
# Link the object files to create the executable
|
||||||
|
# At this point, we do -lcurl to link curl for the request lib
|
||||||
$(TARGET): $(OBJS)
|
$(TARGET): $(OBJS)
|
||||||
$(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJS)
|
$(CXX) $(CXXFLAGS) -lcurl -o $(TARGET) $(OBJS)
|
||||||
|
|
||||||
# Compile .cpp files to .o files in the build directory
|
# Compile .cpp files to .o files in the build directory
|
||||||
$(BUILD_DIR)/%.o: %.cpp
|
$(BUILD_DIR)/%.o: %.cpp
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ InstructionType strToInstructionType(std::string in) {
|
|||||||
else if (in == "file") return InstructionType::File;
|
else if (in == "file") return InstructionType::File;
|
||||||
else if (in == "struct") return InstructionType::Struct;
|
else if (in == "struct") return InstructionType::Struct;
|
||||||
else if (in == "assert") return InstructionType::Assert;
|
else if (in == "assert") return InstructionType::Assert;
|
||||||
|
else if (in == "request") return InstructionType::Request;
|
||||||
else return InstructionType::Variable;
|
else return InstructionType::Variable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
enum class InstructionType {
|
enum class InstructionType {
|
||||||
None, Print, Println, Math, Let, Variable, Exit, If, While, Input, Compare, Function, Struct, Return, Concat, Split, File, Assert
|
None, Print, Println, Math, Let, Variable, Exit, If, While, Input, Compare, Function, Struct, Return, Concat, Split, File, Assert, Request
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class ValueType {
|
enum class ValueType {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
#include "../modules/split/split.h"
|
#include "../modules/split/split.h"
|
||||||
#include "../modules/concat/concat.h"
|
#include "../modules/concat/concat.h"
|
||||||
#include "../modules/file/file.h"
|
#include "../modules/file/file.h"
|
||||||
|
#include "../modules/request/request.h"
|
||||||
#include "../modules/assert/assert.h"
|
#include "../modules/assert/assert.h"
|
||||||
|
|
||||||
// Forward declaration for mutual recursion
|
// Forward declaration for mutual recursion
|
||||||
@@ -257,6 +258,8 @@ Value execute(Instruction inst) {
|
|||||||
return modules::concat(inst.args);
|
return modules::concat(inst.args);
|
||||||
case InstructionType::File:
|
case InstructionType::File:
|
||||||
return modules::file(inst.args);
|
return modules::file(inst.args);
|
||||||
|
case InstructionType::Request:
|
||||||
|
return modules::request(inst.args);
|
||||||
case InstructionType::Exit:
|
case InstructionType::Exit:
|
||||||
return modules::exit(inst.args);
|
return modules::exit(inst.args);
|
||||||
case InstructionType::Return: {
|
case InstructionType::Return: {
|
||||||
|
|||||||
69
src/modules/request/request.cpp
Normal file
69
src/modules/request/request.cpp
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
#include "request.h"
|
||||||
|
#include "../../defs/defs.h"
|
||||||
|
#include "../../error/error.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
|
enum class RequestType {
|
||||||
|
GET, POST
|
||||||
|
};
|
||||||
|
|
||||||
|
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* userp) {
|
||||||
|
size_t totalSize = size * nmemb;
|
||||||
|
userp->append((char*)contents, totalSize);
|
||||||
|
return totalSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string curlWrapper(std::string url, RequestType requestType, std::string data = "") {
|
||||||
|
CURL* curl = curl_easy_init();
|
||||||
|
std::string response;
|
||||||
|
if (curl) {
|
||||||
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, "kyn-libcurl-agent/1.0");
|
||||||
|
|
||||||
|
if (requestType == RequestType::POST) {
|
||||||
|
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
CURLcode res = curl_easy_perform(curl);
|
||||||
|
if (res != CURLE_OK) {
|
||||||
|
error("Request failed: " + std::string(curl_easy_strerror(res)));
|
||||||
|
response = "KYNERR";
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_easy_cleanup(curl);
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
Value modules::request(std::vector<Value> args) {
|
||||||
|
if (args.size() < 2) {
|
||||||
|
error("Not enough args for request module");
|
||||||
|
return Value("Error");
|
||||||
|
}
|
||||||
|
if (args[1].valtype != ValueType::String) {
|
||||||
|
error("Expecting a string to identify request URL");
|
||||||
|
return Value("Error");
|
||||||
|
}
|
||||||
|
if (args[0].valtype == ValueType::Identifier) {
|
||||||
|
if (args[0].string_val == "get") {
|
||||||
|
std::string response = curlWrapper(args[1].string_val, RequestType::GET);
|
||||||
|
if (response == "KYNERR") return Value("Error");
|
||||||
|
return Value(response);
|
||||||
|
} else if (args[0].string_val == "post") {
|
||||||
|
if (args.size() < 3 || args[2].valtype != ValueType::String) {
|
||||||
|
error("post submodule of request expects a string as 2nd arg");
|
||||||
|
return Value("Error");
|
||||||
|
}
|
||||||
|
std::string response = curlWrapper(args[1].string_val, RequestType::POST, args[2].string_val);
|
||||||
|
if (response == "KYNERR") return Value("Error");
|
||||||
|
return Value(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
error("Unknown submodule for request. Only 'get' and 'post' are available.");
|
||||||
|
return Value("Error");
|
||||||
|
}
|
||||||
8
src/modules/request/request.h
Normal file
8
src/modules/request/request.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "../../defs/defs.h"
|
||||||
|
|
||||||
|
namespace modules {
|
||||||
|
Value request(std::vector<Value> args);
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
let counter = 0
|
let counter = 0
|
||||||
|
|
||||||
while compare $counter <= 1000 {
|
while compare $counter <= 10000 {
|
||||||
println $counter
|
println $counter
|
||||||
counter = (math 1 + $counter)
|
counter = (math 1 + $counter)
|
||||||
}
|
}
|
||||||
|
|||||||
33
tests/test.kyn
Normal file
33
tests/test.kyn
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
println "thing"
|
||||||
|
|
||||||
|
if compare 5 == 10 {
|
||||||
|
println "uh oh its broken"
|
||||||
|
} else {
|
||||||
|
println "its working"
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Dingus {
|
||||||
|
name = <String>
|
||||||
|
age = <Int>
|
||||||
|
fun init name age {
|
||||||
|
self name = name
|
||||||
|
self age = age
|
||||||
|
}
|
||||||
|
|
||||||
|
fun tostring {
|
||||||
|
return (concat name ", " age)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let myDingus = (Dingus "dingus" 23)
|
||||||
|
|
||||||
|
fun myFn param1 param2 andSoOn {
|
||||||
|
println param1
|
||||||
|
return param2
|
||||||
|
}
|
||||||
|
|
||||||
|
println (myFn 123 "hi" 3.14)
|
||||||
|
|
||||||
|
let favNumbers = [7, 14, 21]
|
||||||
|
|
||||||
|
println (file read "README.md")
|
||||||
Reference in New Issue
Block a user