Request module

This commit is contained in:
2025-10-06 11:57:49 +11:00
parent e3b24b585b
commit faebaf7d3f
8 changed files with 118 additions and 3 deletions

View File

@@ -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

View File

@@ -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;
} }

View File

@@ -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 {

View File

@@ -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: {

View 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");
}

View File

@@ -0,0 +1,8 @@
#pragma once
#include <vector>
#include "../../defs/defs.h"
namespace modules {
Value request(std::vector<Value> args);
}

View File

@@ -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
View 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")