2025-09-17 07:43:07 +09:00
|
|
|
|
/*!
|
2025-09-20 09:11:52 +09:00
|
|
|
|
Nyash Programming Language — Rust library crate.
|
|
|
|
|
|
Provides parser, MIR, backends, runner, and supporting runtime.
|
|
|
|
|
|
*/
|
2025-08-09 15:14:44 +09:00
|
|
|
|
|
2025-09-21 08:53:00 +09:00
|
|
|
|
// Allow referring to this crate as `nyash_rust` from within the crate, matching external paths.
|
|
|
|
|
|
extern crate self as nyash_rust;
|
|
|
|
|
|
|
2025-09-20 09:11:52 +09:00
|
|
|
|
// WebAssembly support
|
2025-08-09 15:14:44 +09:00
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
2025-09-21 08:53:00 +09:00
|
|
|
|
// Legacy interpreter removed
|
2025-09-17 10:58:12 +09:00
|
|
|
|
mod interpreter_stub;
|
|
|
|
|
|
|
2025-09-20 09:11:52 +09:00
|
|
|
|
pub mod ast; // using historical ast.rs
|
2025-09-17 07:43:07 +09:00
|
|
|
|
pub mod box_arithmetic;
|
2025-09-20 09:11:52 +09:00
|
|
|
|
pub mod box_factory; // unified Box Factory
|
|
|
|
|
|
pub mod box_operators; // operator implementations for basic Box types
|
2025-08-09 15:14:44 +09:00
|
|
|
|
pub mod box_trait;
|
|
|
|
|
|
pub mod boxes;
|
|
|
|
|
|
pub mod channel_box;
|
2025-09-20 09:11:52 +09:00
|
|
|
|
pub mod core; // core models shared by backends
|
2025-09-17 07:43:07 +09:00
|
|
|
|
pub mod environment;
|
2025-08-09 15:14:44 +09:00
|
|
|
|
pub mod exception_box;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
pub mod finalization;
|
2025-09-20 09:11:52 +09:00
|
|
|
|
pub mod instance_v2; // simplified InstanceBox implementation
|
2025-09-17 10:58:12 +09:00
|
|
|
|
pub mod interpreter { pub use crate::interpreter_stub::*; }
|
2025-08-09 15:14:44 +09:00
|
|
|
|
pub mod method_box;
|
2025-09-20 09:11:52 +09:00
|
|
|
|
pub mod operator_traits; // trait-based operator overloading
|
|
|
|
|
|
pub mod parser; // using historical parser.rs
|
|
|
|
|
|
pub mod scope_tracker; // Box lifecycle tracking for VM
|
2025-09-17 07:43:07 +09:00
|
|
|
|
pub mod stdlib;
|
|
|
|
|
|
pub mod tokenizer;
|
2025-09-20 09:11:52 +09:00
|
|
|
|
pub mod type_box; // TypeBox system (arithmetic moved from box_trait.rs)
|
2025-08-09 15:14:44 +09:00
|
|
|
|
|
2025-08-12 10:08:30 +00:00
|
|
|
|
pub mod value;
|
|
|
|
|
|
|
2025-08-12 01:35:36 +00:00
|
|
|
|
pub mod messaging;
|
|
|
|
|
|
pub mod transport;
|
|
|
|
|
|
|
2025-09-20 09:11:52 +09:00
|
|
|
|
// MIR (Mid-level Intermediate Representation)
|
2025-08-12 11:33:48 +00:00
|
|
|
|
pub mod mir;
|
2025-09-07 07:36:15 +09:00
|
|
|
|
#[cfg(feature = "aot-plan-import")]
|
|
|
|
|
|
pub mod mir_aot_plan_import {
|
|
|
|
|
|
pub use crate::mir::aot_plan_import::*;
|
|
|
|
|
|
}
|
2025-08-12 11:33:48 +00:00
|
|
|
|
|
2025-09-20 09:11:52 +09:00
|
|
|
|
// Backends
|
2025-08-13 10:26:46 +00:00
|
|
|
|
pub mod backend;
|
2025-09-20 09:11:52 +09:00
|
|
|
|
pub mod jit; // Cranelift JIT subsystem (skeleton)
|
2025-09-01 23:44:34 +09:00
|
|
|
|
pub mod semantics; // Unified semantics trait for MIR evaluation/lowering
|
2025-08-13 10:26:46 +00:00
|
|
|
|
|
2025-08-14 07:19:23 +09:00
|
|
|
|
pub mod benchmarks;
|
|
|
|
|
|
|
2025-08-18 11:07:03 +09:00
|
|
|
|
// BID-FFI / Plugin system (prototype)
|
|
|
|
|
|
pub mod bid;
|
|
|
|
|
|
|
2025-08-19 03:48:44 +09:00
|
|
|
|
// Configuration system
|
|
|
|
|
|
pub mod config;
|
|
|
|
|
|
|
2025-08-19 05:36:01 +09:00
|
|
|
|
// CLI system
|
|
|
|
|
|
pub mod cli;
|
|
|
|
|
|
|
2025-08-19 03:48:44 +09:00
|
|
|
|
// Runtime system (plugins, registry, etc.)
|
2025-08-26 05:49:23 +09:00
|
|
|
|
pub mod debug;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
pub mod runner_plugin_init;
|
|
|
|
|
|
pub mod runtime;
|
2025-09-20 09:11:52 +09:00
|
|
|
|
// Unified Grammar scaffolding
|
2025-09-02 17:12:51 +09:00
|
|
|
|
pub mod grammar;
|
2025-09-20 09:11:52 +09:00
|
|
|
|
pub mod syntax; // syntax sugar config and helpers
|
2025-09-21 08:53:00 +09:00
|
|
|
|
// Execution runner (CLI coordinator)
|
|
|
|
|
|
pub mod runner;
|
|
|
|
|
|
|
|
|
|
|
|
// Expose the macro engine module under a raw identifier; the source lives under `src/macro/`.
|
|
|
|
|
|
#[path = "macro/mod.rs"]
|
|
|
|
|
|
pub mod r#macro;
|
2025-08-19 03:48:44 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
|
|
pub mod wasm_test;
|
|
|
|
|
|
|
2025-08-10 03:28:59 +00:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
pub mod tests;
|
|
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
// Re-export main types for easy access
|
|
|
|
|
|
pub use ast::{ASTNode, BinaryOperator, LiteralValue};
|
2025-09-17 07:43:07 +09:00
|
|
|
|
pub use box_arithmetic::{AddBox, CompareBox, DivideBox, ModuloBox, MultiplyBox, SubtractBox};
|
|
|
|
|
|
pub use box_trait::{BoolBox, IntegerBox, NyashBox, StringBox, VoidBox};
|
|
|
|
|
|
pub use environment::{Environment, PythonCompatEnvironment};
|
2025-09-17 10:58:12 +09:00
|
|
|
|
#[cfg(feature = "interpreter-legacy")]
|
|
|
|
|
|
#[cfg(feature = "interpreter-legacy")]
|
2025-08-09 15:14:44 +09:00
|
|
|
|
pub use interpreter::{NyashInterpreter, RuntimeError};
|
2025-09-17 07:43:07 +09:00
|
|
|
|
pub use parser::{NyashParser, ParseError};
|
|
|
|
|
|
pub use tokenizer::{NyashTokenizer, Token, TokenType};
|
|
|
|
|
|
pub use type_box::{MethodSignature, TypeBox, TypeRegistry}; // 🌟 TypeBox exports
|
|
|
|
|
|
// pub use instance::InstanceBox; // 旧実装
|
|
|
|
|
|
pub use boxes::console_box::ConsoleBox;
|
|
|
|
|
|
pub use boxes::debug_box::DebugBox;
|
2025-08-09 15:14:44 +09:00
|
|
|
|
pub use boxes::map_box::MapBox;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
pub use boxes::math_box::{FloatBox, MathBox, RangeBox};
|
|
|
|
|
|
pub use boxes::null_box::{null, NullBox};
|
2025-08-09 15:14:44 +09:00
|
|
|
|
pub use boxes::random_box::RandomBox;
|
|
|
|
|
|
pub use boxes::sound_box::SoundBox;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
pub use boxes::time_box::{DateTimeBox, TimeBox, TimerBox};
|
|
|
|
|
|
pub use channel_box::{ChannelBox, MessageBox};
|
|
|
|
|
|
pub use instance_v2::InstanceBox; // 🎯 新実装テスト(nyash_rustパス使用)
|
|
|
|
|
|
pub use method_box::{BoxType, EphemeralInstance, FunctionDefinition, MethodBox};
|
2025-08-09 15:14:44 +09:00
|
|
|
|
|
2025-08-12 10:08:30 +00:00
|
|
|
|
pub use value::NyashValue;
|
|
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
// Direct canvas test export
|
2025-09-17 10:58:12 +09:00
|
|
|
|
#[cfg(all(target_arch = "wasm32", feature = "interpreter-legacy"))]
|
2025-08-09 15:14:44 +09:00
|
|
|
|
pub use wasm_test::wasm_test::test_direct_canvas_draw;
|
|
|
|
|
|
|
2025-09-20 09:11:52 +09:00
|
|
|
|
// WebAssembly exports for browser usage
|
2025-09-17 10:58:12 +09:00
|
|
|
|
#[cfg(all(target_arch = "wasm32", feature = "interpreter-legacy"))]
|
2025-08-09 15:14:44 +09:00
|
|
|
|
#[wasm_bindgen]
|
|
|
|
|
|
pub struct NyashWasm {
|
|
|
|
|
|
interpreter: NyashInterpreter,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-17 10:58:12 +09:00
|
|
|
|
#[cfg(all(target_arch = "wasm32", feature = "interpreter-legacy"))]
|
2025-08-09 15:14:44 +09:00
|
|
|
|
#[wasm_bindgen]
|
|
|
|
|
|
impl NyashWasm {
|
|
|
|
|
|
/// Create a new Nyash interpreter instance for browser use
|
|
|
|
|
|
#[wasm_bindgen(constructor)]
|
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
|
// Setup panic handling for better browser debugging
|
|
|
|
|
|
console_error_panic_hook::set_once();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
// Create interpreter with browser-specific setup
|
2025-08-30 08:54:15 +09:00
|
|
|
|
let interpreter = NyashInterpreter::new();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
// Register browser-specific boxes
|
|
|
|
|
|
// ConsoleBox is available as a constructor: console = new ConsoleBox()
|
|
|
|
|
|
// TODO: Also register DOMBox, CanvasBox etc.
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
Self { interpreter }
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
/// Evaluate Nyash code and return result as string
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
|
|
pub fn eval(&mut self, code: &str) -> String {
|
|
|
|
|
|
// Handle empty or whitespace-only input
|
|
|
|
|
|
let trimmed_code = code.trim();
|
|
|
|
|
|
if trimmed_code.is_empty() {
|
|
|
|
|
|
return String::new();
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
// Split multiline code into logical statements for better WASM handling
|
2025-09-17 07:43:07 +09:00
|
|
|
|
let lines: Vec<&str> = trimmed_code
|
|
|
|
|
|
.lines()
|
2025-08-09 15:14:44 +09:00
|
|
|
|
.map(|line| line.trim())
|
|
|
|
|
|
.filter(|line| !line.is_empty() && !line.starts_with("//"))
|
|
|
|
|
|
.collect();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
// If single line or looks like a complete static box/box definition, parse as-is
|
2025-09-17 07:43:07 +09:00
|
|
|
|
if lines.len() == 1 || trimmed_code.contains("static box") || trimmed_code.contains("box ")
|
|
|
|
|
|
{
|
2025-08-09 15:14:44 +09:00
|
|
|
|
return self.eval_single_block(trimmed_code);
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
// For multiple lines, try to execute line by line
|
|
|
|
|
|
let mut results = Vec::new();
|
|
|
|
|
|
let mut accumulated_code = String::new();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
for line in lines {
|
|
|
|
|
|
// Accumulate lines for block structures
|
|
|
|
|
|
accumulated_code.push_str(line);
|
|
|
|
|
|
accumulated_code.push('\n');
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
// Check if we have a complete statement
|
|
|
|
|
|
if self.is_complete_statement(&accumulated_code) {
|
|
|
|
|
|
let result = self.eval_single_block(accumulated_code.trim());
|
|
|
|
|
|
if result.starts_with("Parse Error:") {
|
|
|
|
|
|
return result; // Stop on parse error
|
|
|
|
|
|
}
|
|
|
|
|
|
if !result.is_empty() && result != "void" {
|
|
|
|
|
|
results.push(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
accumulated_code.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
// Execute any remaining accumulated code
|
|
|
|
|
|
if !accumulated_code.trim().is_empty() {
|
|
|
|
|
|
let result = self.eval_single_block(accumulated_code.trim());
|
|
|
|
|
|
if !result.is_empty() && result != "void" {
|
|
|
|
|
|
results.push(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
// Return the most relevant result
|
2025-09-17 07:43:07 +09:00
|
|
|
|
results
|
|
|
|
|
|
.into_iter()
|
2025-08-09 15:14:44 +09:00
|
|
|
|
.filter(|r| !r.starts_with("Parse Error:") && !r.starts_with("Runtime Error:"))
|
|
|
|
|
|
.last()
|
|
|
|
|
|
.unwrap_or_else(|| "void".to_string())
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
/// Evaluate a single block of code
|
|
|
|
|
|
fn eval_single_block(&mut self, code: &str) -> String {
|
|
|
|
|
|
// First parse the code into an AST
|
|
|
|
|
|
let ast = match NyashParser::parse_from_string(code) {
|
|
|
|
|
|
Ok(ast) => ast,
|
|
|
|
|
|
Err(e) => return format!("Parse Error: {}", e),
|
|
|
|
|
|
};
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
// Then execute the AST
|
|
|
|
|
|
match self.interpreter.execute(ast) {
|
|
|
|
|
|
Ok(result_box) => {
|
|
|
|
|
|
// Format the result for browser display
|
|
|
|
|
|
let result_str = result_box.to_string_box().value;
|
|
|
|
|
|
if result_str == "void" || result_str.is_empty() {
|
|
|
|
|
|
"void".to_string()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
result_str
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
Err(e) => format!("Runtime Error: {}", e),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
/// Check if code represents a complete statement (heuristic)
|
|
|
|
|
|
fn is_complete_statement(&self, code: &str) -> bool {
|
|
|
|
|
|
let trimmed = code.trim();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
// Always complete: assignments, function calls, simple expressions
|
|
|
|
|
|
if trimmed.contains('=') && !trimmed.ends_with('=') {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
// Block structures need closing braces
|
|
|
|
|
|
let open_braces = trimmed.chars().filter(|&c| c == '{').count();
|
|
|
|
|
|
let close_braces = trimmed.chars().filter(|&c| c == '}').count();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
// Complete if braces are balanced or no braces at all
|
|
|
|
|
|
open_braces == 0 || open_braces == close_braces
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-09 15:14:44 +09:00
|
|
|
|
/// Get the current version info
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
|
|
pub fn version() -> String {
|
|
|
|
|
|
String::from("Nyash WASM v0.1.0 - Everything is Box in Browser!")
|
|
|
|
|
|
}
|
2025-08-18 11:07:03 +09:00
|
|
|
|
}
|