Meta: Add files to repository

This commit is contained in:
Alec Murphy
2025-03-25 07:32:23 -04:00
parent 80a0428b66
commit 39198164cd
1029 changed files with 78311 additions and 0 deletions

350
src/net/lib/json.jakt Normal file
View File

@@ -0,0 +1,350 @@
/// Expect:
/// - output: "JsonValue::JsonArray([JsonValue::Object([\"id\": JsonValue::Number(0.5), \"displayName\": JsonValue::JsonString(\"Air\"), \"name\": JsonValue::JsonString(\"air\"), \"hardness\": JsonValue::Number(3.9), \"resistance\": JsonValue::Number(0), \"minStateId\": JsonValue::Number(0), \"maxStateId\": JsonValue::Number(0), \"states\": JsonValue::JsonArray([])])])\n"
enum JsonValue {
Null
Bool(bool)
Number(f64)
// FIXME: This variant should be called String
JsonString(String)
// FIXME: This variant should be called Array
JsonArray([JsonValue])
Object([String:JsonValue])
}
fn is_whitespace(anon c: u8) -> bool {
return match c {
b'\t' | b'\n' | b'\r' | b' ' => true
else => false
}
}
class JsonParser {
input: String
index: usize
public fn construct(input: String) throws -> JsonParser {
return JsonParser(input, index: 0)
}
fn eof(this) -> bool {
return .index >= .input.length()
}
public fn parse(mut this) throws -> JsonValue {
// FIXME: Jakt::JsonParser ignores trailing whitespace for some reason.
let value = .parse_helper()
if not .eof() {
// FIXME: "Didn't consume all input"
throw Error::from_errno(9000)
}
return value
}
fn skip_whitespace(mut this) {
while not .eof() {
if not is_whitespace(.input.byte_at(.index)) {
break
}
.index++
}
}
fn consume_and_unescape_string(mut this) throws -> String {
if not .consume_specific(b'"') {
// FIXME: "Expected '"'
throw Error::from_errno(9007)
}
mut builder = StringBuilder::create()
loop {
mut ch = 0u8
mut peek_index = .index
while peek_index < .input.length() {
ch = .input.byte_at(peek_index)
if ch == b'"' or ch == b'\\' {
break
}
// FIXME: This is is_ascii_c0_control()
if ch < 0x20 {
// FIXME: "Error while parsing string"
throw Error::from_errno(9008)
}
peek_index++
}
while peek_index != .index {
builder.append(.input.byte_at(.index))
.index++
}
if .eof() {
break
}
if ch == b'"' {
break
}
if ch != b'\\' {
builder.append(.consume())
continue
}
.ignore()
match .peek() {
b'"' | b'/' | b'\\' | b'n' | b'r' | b't' | b'b' | b'f' => {
let ch = .consume()
builder.append(match ch {
b'n' => b'\n'
b'r' => b'\r'
b't' => b'\t'
b'b' => b'\b'
b'f' => b'\f'
else => ch
})
}
b'u' => {
eprintln("FIXME: Implement unicode literals")
abort()
}
else => {
// FIXME: "Error while parsing string"
throw Error::from_errno(9009)
}
}
}
if not .consume_specific(b'"') {
// FIXME: "Expected '"'"
throw Error::from_errno(9010)
}
return builder.to_string()
}
fn ignore(mut this) {
.index++
}
fn peek(this) -> u8 {
if .eof() {
return 0
}
return .input.byte_at(.index)
}
fn consume(mut this) -> u8 {
let ch = .peek()
.index++
return ch
}
fn consume_specific(mut this, anon expected: u8) -> bool {
if .peek() != expected {
return false
}
.index++
return true
}
fn parse_helper(mut this) throws -> JsonValue {
.skip_whitespace()
return match .peek() {
b'{' => .parse_object()
b'[' => .parse_array()
b'"' => .parse_string()
b'-' => .parse_number()
b'0' | b'1' | b'2' | b'3' | b'4' | b'5' | b'6' | b'7' | b'8' | b'9' => .parse_number()
b'f' => .parse_false()
b't' => .parse_true()
b'n' => .parse_null()
else => .parse_failure(error_message: "Unexpected character")
}
}
fn parse_failure(this, error_message: String) throws -> JsonValue {
throw Error::from_errno(9001)
}
fn parse_array(mut this) throws -> JsonValue {
mut array: [JsonValue] = []
if (not .consume_specific(b'[')) {
// Expected '['
throw Error::from_errno(9014)
}
loop {
.skip_whitespace()
if .peek() == b']' {
break
}
array.push(.parse_helper())
.skip_whitespace()
if .peek() == b']' {
break
}
if not .consume_specific(b',') {
// Expected ','
throw Error::from_errno(9014)
}
.skip_whitespace()
if .peek() == b']' {
// Unexpected ']'
throw Error::from_errno(9014)
}
}
if not .consume_specific(b']') {
// Expected ']'
throw Error::from_errno(9015)
}
return JsonValue::JsonArray(array)
}
fn parse_object(mut this) throws -> JsonValue {
if not .consume_specific(b'{') {
// FIXME: "Expected '{'"
throw Error::from_errno(9002)
}
mut values: [String:JsonValue] = [:]
loop {
.skip_whitespace()
if .peek() == b'}' {
break
}
.skip_whitespace()
let key = .consume_and_unescape_string()
.skip_whitespace()
if not .consume_specific(b':') {
// FIXME: "Expected ':'"
throw Error::from_errno(9003)
}
.skip_whitespace()
let value = .parse_helper()
// FIXME: This should say `values[key] = value`, but the compiler doesn't wrap it in TRY()
values.set(key, value)
.skip_whitespace()
if .peek() == b'}' {
break
}
if not .consume_specific(b',') {
// FIXME: "Expected ','"
throw Error::from_errno(9004)
}
.skip_whitespace()
if .peek() == b'}' {
// FIXME: "Unexpected '}'"
throw Error::from_errno(9005)
}
}
if not .consume_specific(b'}') {
// FIXME: "Expected '}'"
throw Error::from_errno(9006)
}
return JsonValue::Object(values)
}
fn char_to_f64(anon num: u8) throws -> f64 {
// FIXME 1: Shouldn't need this function at all
// FIXME 2: Shouldn't need return in else branch
return match num {
0u8 => 0.0
1u8 => 1.0
2u8 => 2.0
3u8 => 3.0
4u8 => 4.0
5u8 => 5.0
6u8 => 6.0
7u8 => 7.0
8u8 => 8.0
9u8 => 9.0
else => {
// FIXME: "Unexpected number"
throw Error::from_errno(9017)
}
}
}
fn parse_number(mut this) throws -> JsonValue {
// FIXME: This implementation doesn't match JsonParser.cpp
let is_negative = .consume_specific(b'-')
mut decimal_start_index: usize? = None
mut value = 0.0
while not .eof() {
let ch = .peek()
if ch == b'.' {
if decimal_start_index.has_value() {
// FIXME: "Unexpected '.'"
throw Error::from_errno(9016)
}
decimal_start_index = .index++
continue
} else if not (ch >= b'0' and ch <= b'9') {
break
}
if not decimal_start_index.has_value() {
value *= 10.0
value += char_to_f64(ch - b'0')
} else {
mut num = char_to_f64(ch - b'0')
// FIXME: This should really be: `value += pow(10, -decimal_place)*num`, but: there's no pow function and you can't multiply float by usize
let decimal_place = .index - decimal_start_index.value()
for i in 0..decimal_place {
num /= 10.0
}
value += num
}
.index++
}
if is_negative {
value *= -1.0
}
return JsonValue::Number(value)
}
fn parse_string(mut this) throws -> JsonValue {
return JsonValue::JsonString(.consume_and_unescape_string())
}
fn parse_false(mut this) throws -> JsonValue {
if (.consume() != b'f' or .consume() != b'a' or .consume() != b'l' or .consume() != b's' or .consume() != b'e') {
// FIXME: "Expected 'false'"
throw Error::from_errno(9011)
}
return JsonValue::Bool(false)
}
fn parse_true(mut this) throws -> JsonValue {
if (.consume() != b't' or .consume() != b'r' or .consume() != b'u' or .consume() != b'e') {
// FIXME: "Expected 'true'"
throw Error::from_errno(9012)
}
return JsonValue::Bool(true)
}
fn parse_null(mut this) throws -> JsonValue {
if (.consume() != b'n' or .consume() != b'u' or .consume() != b'l' or .consume() != b'l') {
// FIXME: "Expected 'null'"
throw Error::from_errno(9013)
}
return JsonValue::Null
}
}
// fn parse_json(input: String) throws -> JsonValue {
// mut parser = JsonParser::construct(input)
// return parser.parse()
// }
//
// fn main() {
// let value = parse_json(input: "[{\"id\":0.5,\"displayName\":\"Air\",\"name\":\"air\",\"hardness\":3.9,\"resistance\":0,\"minStateId\":0,\"maxStateId\":0,\"states\":[]}]")
// println("{}", value)
// }

147
src/net/lib/util.jakt Normal file
View File

@@ -0,0 +1,147 @@
import relative parent::os::os { OS }
struct Util {
fn get_address_u32_from_ipv4_u8_array(anon array: [u8]) -> u32 {
if array.size() != 4 {
return 0
}
mut address: u32 = (array[3] as! u32 & 0xff) as! u32
address += ((array[2] as! u32 & 0xff) << 8) as! u32
address += ((array[1] as! u32 & 0xff) << 16) as! u32
address += ((array[0] as! u32 & 0xff) << 24) as! u32
return address
}
fn get_hexadecimal_string_from_ipv4_u8_array(anon array: [u8]) throws -> String {
mut s = StringBuilder::create()
unsafe {
cpp {
"char *chars = (char*)calloc(32, 1);
sprintf(chars, \"%02x%02x%02x%02x\", array[0], array[1], array[2], array[3]);
s.append_c_string(chars);
delete(chars);"
}
}
return s.to_string()
}
fn get_md5_string_from_string(anon s: String) throws -> String {
mut sb = StringBuilder::create()
unsafe {
cpp {
"
char* md5 = (char*)os_call((u64)\"@saubari_get_md5_string_from_string\", (u64)s.characters());
sb.append_c_string(md5);
delete(md5);
"
}
}
return sb.to_string()
}
fn get_ipv4_u8_array_from_address_string(anon s: String) throws -> [u8] {
mut address: [u8] = []
let octet_strings = s.split(c'.')
for octet_string in octet_strings {
unsafe {
cpp {
"auto value = octet_string.to_number<u32>();
if (value.has_value()) {
auto result = value.release_value();
address.push(result & 0xff);
}"
}
}
}
return address
}
fn get_ipv4_u8_array_from_address_u32(anon addr: u32) throws -> [u8] {
mut address: [u8] = []
// let source_address: [u8] = [ipv4_packet[12], ipv4_packet[13], ipv4_packet[14], ipv4_packet[15]]
address.push(((addr >> 24) & 0xff) as! u8)
address.push(((addr >> 16) & 0xff) as! u8)
address.push(((addr >> 8) & 0xff) as! u8)
address.push((addr & 0xff) as! u8)
return address
}
fn get_string_from_u8_array(anon array: [u8]) throws -> String {
mut s = StringBuilder::create()
unsafe {
cpp {
"for (int i = 0; i < array.size(); i++) {
s.append(array[i]);
}"
}
}
return s.to_string()
}
fn get_u16_from_u8_array(anon array: [u8], anon offset: i64) -> u16{
return (array[offset] as! u16 << 8) + array[offset + 1] as! u16
}
fn get_u16_from_u8_arrayslice(anon array: ArraySlice<u8>, anon offset: i64) -> u16{
return (array[offset] as! u16 << 8) + array[offset + 1] as! u16
}
fn push_string_to_u8_array(anon mut array: [u8], anon s: String) throws {
for i in 0..s.length() {
unsafe {
cpp {
"array.push(s.characters()[i]);"
}
}
}
}
fn push_u16_to_u8_array(anon mut array: [u8], anon value: u16) throws {
array.push((value >> 8) as! u8)
array.push((value & 0xff) as! u8)
}
fn push_u32_to_u8_array(anon mut array: [u8], anon value: u32) throws {
mut val_u32_to_u8: u32 = 0
val_u32_to_u8 = (value >> 24) & 0xff
array.push(val_u32_to_u8 as! u8)
val_u32_to_u8 = (value >> 16) & 0xff
array.push(val_u32_to_u8 as! u8)
val_u32_to_u8 = (value >> 8) & 0xff
array.push(val_u32_to_u8 as! u8)
array.push((value & 0xff) as! u8)
}
fn get_dictionary_from_json_file(anon json_file: String) throws -> [String:String] {
mut dictionary: [String:String] = Dictionary()
let json_bytes = OS::read_entire_file(json_file)
let json_string = get_string_from_u8_array(json_bytes)
unsafe {
cpp {
"auto json = JsonValue::from_string(json_string).value();
auto const& object = json.as_object();
object.for_each_member([&]([[maybe_unused]] auto& property_name, [[maybe_unused]] const JsonValue& property_value) {
dictionary.set(property_name, property_value.deprecated_to_byte_string());
});"
}
}
return dictionary
}
fn get_dictionary_from_string(anon s: String) throws -> [String:String] {
mut dictionary: [String:String] = Dictionary()
unsafe {
cpp {
"auto json = JsonValue::from_string(s).value();
auto const& object = json.as_object();
object.for_each_member([&]([[maybe_unused]] auto& property_name, [[maybe_unused]] const JsonValue& property_value) {
dictionary.set(property_name, property_value.deprecated_to_byte_string());
});"
}
}
return dictionary
}
fn string_from_file(anon filepath: String) throws -> String {
if filepath.is_empty() or not OS::path_exists(filepath) {
return ""
}
let array = OS::read_entire_file(filepath)
mut s = StringBuilder::create()
unsafe {
cpp {
"for (int i = 0; i < array.size(); i++) {
s.append(array[i]);
}"
}
}
return s.to_string()
}
}