From 21e963168e0a7c33f4483e025121adf252a613dd Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Thu, 3 Sep 2026 23:56:19 +0200 Subject: [PATCH] feat(furas): add slice type and instruction --- furas/examples/array2.furas | 8 ++++++-- furas/include/furas/token.hpp | 1 + furas/src/gen.cpp | 15 +++++++++++++++ furas/src/lexer.cpp | 1 + 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/furas/examples/array2.furas b/furas/examples/array2.furas index 7f6cec9..ee3b352 100644 --- a/furas/examples/array2.furas +++ b/furas/examples/array2.furas @@ -1,6 +1,7 @@ type arr = array $s8 10 +type arrSlice = slice $s8 -func println ref $arr = native println +func println $arrSlice = native println public func main = #main main: @@ -30,5 +31,8 @@ body: end: drop load %0 - call ref $arr println + push $u32 0 + load %1 + slice + call $arrSlice println ret diff --git a/furas/include/furas/token.hpp b/furas/include/furas/token.hpp index b50edb7..07f5b31 100644 --- a/furas/include/furas/token.hpp +++ b/furas/include/furas/token.hpp @@ -34,6 +34,7 @@ struct token { // Instructions Push, Array, + Slice, Get, Set, Drop, diff --git a/furas/src/gen.cpp b/furas/src/gen.cpp index 6d6c9ee..b86efb2 100644 --- a/furas/src/gen.cpp +++ b/furas/src/gen.cpp @@ -4,6 +4,7 @@ #include "furvm/function.hpp" #include "furvm/fwd.hpp" #include "furvm/instruction.hpp" +#include "furvm/module.hpp" #include #include @@ -24,6 +25,7 @@ namespace { // NOLINTBEGIN std::unordered_map instructions = { { token::Array, furvm::instruction_t::Array }, + { token::Slice, furvm::instruction_t::Slice }, { token::Get, furvm::instruction_t::Get }, { token::Set, furvm::instruction_t::Set }, { token::Drop, furvm::instruction_t::Drop }, @@ -77,6 +79,7 @@ const char* token_type(enum token::type type) { case token::Allocate: return "allocate"; case token::Push: return "push"; case token::Array: return "array"; + case token::Slice: return "slice"; case token::Get: return "get"; case token::Set: return "set"; case token::Drop: return "drop"; @@ -160,6 +163,8 @@ struct mod_context { case furvm::mod_type::Array: return lhs->value.array.size == rhs->value.array.size && compare_types(mod.type_at(lhs->value.array.typeId), mod.type_at(rhs->value.array.typeId)); + case furvm::mod_type::Slice: + return compare_types(mod.type_at(lhs->value.slice.typeId), mod.type_at(rhs->value.slice.typeId)); case furvm::mod_type::Import: return lhs->value.imprt.modId == rhs->value.imprt.modId && lhs->value.imprt.typeId == rhs->value.imprt.typeId; @@ -252,6 +257,15 @@ struct mod_context { return mod.emplace_type(inner.id(), size->value.uint); } + case token::Slice: { + auto result = next_token(lexer); + + if (!result) throw std::runtime_error("error"); + + auto inner = eat_type(lexer, result.value); + + return mod.emplace_type(furvm::mod_type::Slice, inner.id()); + } default: throw std::runtime_error("error"); } } @@ -463,6 +477,7 @@ struct mod_context { } case token::Array: + case token::Slice: case token::Get: case token::Set: case token::Drop: diff --git a/furas/src/lexer.cpp b/furas/src/lexer.cpp index 7278dec..fcb1b64 100644 --- a/furas/src/lexer.cpp +++ b/furas/src/lexer.cpp @@ -43,6 +43,7 @@ token_r lexer::next_token() { { "push", token::Push }, { "array", token::Array }, + { "slice", token::Slice }, { "get", token::Get }, { "set", token::Set }, { "drop", token::Drop },