Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions docs/modules.html
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,17 @@ <h1 class="title">Waspy Development Board</h1>
{ name: "sum() - iterable with start value", status: "done", version: "0.7.0" }
]
},
imports: {
title: "IMPORT SYSTEM",
runtimeFeatures: {
title: "RUNTIME FEATURES",
status: "progress",
version: "0.5.0",
version: "in-dev",
features: [
{ name: "Import syntax parsing (all types)", status: "done", version: "0.5.0" },
{ name: "Conditional imports (try/except)", status: "done", version: "0.5.0" },
{ name: "Dynamic imports (__import__, importlib)", status: "done", version: "0.5.0" },
{ name: "Actual module loading & execution", status: "todo", version: "todo" }
{ name: "Generator State Management - execution context suspension/resumption", status: "progress" },
{ name: "Dynamic Module Loading - runtime module loading and execution", status: "progress" },
{ name: "Closure Variable Capture Analysis - detecting and capturing closure variables", status: "progress" },
{ name: "String Transformation in WASM - full character-by-char transformations", status: "progress" },
{ name: "Set Hashing & Storage - proper hash table implementation for sets", status: "progress" },
{ name: "Dictionary Index Assignment with Update - modifying dictionary values", status: "progress" }
]
},

Expand All @@ -273,12 +275,19 @@ <h1 class="title">Waspy Development Board</h1>
},
stdlib: {
title: "STANDARD LIBRARY",
status: "todo",
status: "progress",
version: "in-dev",
features: [
{ name: "sys module (system parameters)", status: "todo" },
{ name: "os module (operating system interface)", status: "todo" },
{ name: "math module (mathematical functions)", status: "todo" },
{ name: "json module (JSON encoding/decoding)", status: "todo" }
{ name: "sys module (argv, platform, version, maxsize, stdin/stdout/stderr, path)", status: "done", version: "unreleased" },
{ name: "os module (operating system interface)", status: "progress" },
{ name: "math module (mathematical functions)", status: "progress" },
{ name: "random module (random number generation)", status: "progress" },
{ name: "json module (JSON encoding/decoding)", status: "progress" },
{ name: "re module (regular expressions)", status: "progress" },
{ name: "datetime module (date and time handling)", status: "progress" },
{ name: "collections module (specialized container types)", status: "progress" },
{ name: "itertools module (iterator utilities)", status: "progress" },
{ name: "functools module (higher-order functions)", status: "progress" }
]
},
comprehensions: {
Expand Down
7 changes: 7 additions & 0 deletions examples/test_sys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sys

def test_sys():
platform = sys.platform
version = sys.version
maxsize = sys.maxsize
return maxsize
42 changes: 34 additions & 8 deletions src/compiler/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1341,38 +1341,64 @@ pub fn emit_expr(
}
}
IRExpr::Attribute { object, attribute } => {
// Evaluate object to get pointer
if let IRExpr::Variable(module_name) = &**object {
if crate::stdlib::is_stdlib_module(module_name) {
if let Some(value) =
crate::stdlib::get_stdlib_attributes(module_name, attribute)
{
return match value {
crate::stdlib::StdlibValue::Int(i) => {
func.instruction(&Instruction::I32Const(i));
IRType::Int
}
crate::stdlib::StdlibValue::String(s) => {
let offset =
memory_layout.string_offsets.get(&s).copied().unwrap_or(0);
func.instruction(&Instruction::I32Const(offset as i32));
func.instruction(&Instruction::I32Const(s.len() as i32));
IRType::String
}
crate::stdlib::StdlibValue::Float(f) => {
func.instruction(&Instruction::F64Const(f.into()));
IRType::Float
}
crate::stdlib::StdlibValue::List(_) => {
func.instruction(&Instruction::I32Const(10000));
IRType::List(Box::new(IRType::String))
}
crate::stdlib::StdlibValue::None => {
func.instruction(&Instruction::I32Const(0));
IRType::None
}
};
}
}
}

let obj_type = emit_expr(object, func, ctx, memory_layout, None);

match &obj_type {
IRType::Class(class_name) => {
// Get class info to find field offset
if let Some(class_info) = ctx.get_class_info(class_name) {
if let Some(&field_offset) = class_info.field_offsets.get(attribute) {
// Stack: ...object_ptr
// Load from object_ptr + field_offset
func.instruction(&Instruction::I32Load(MemArg {
offset: field_offset,
align: 2,
memory_index: 0,
}));
// For now, return as Int (could be any type)
IRType::Unknown
} else {
// Field not found in class
func.instruction(&Instruction::Drop);
func.instruction(&Instruction::I32Const(0));
IRType::Unknown
}
} else {
// Class not found
func.instruction(&Instruction::Drop);
func.instruction(&Instruction::I32Const(0));
IRType::Unknown
}
}
_ => {
// Non-class attribute access (strings, lists handled separately)
emit_expr(object, func, ctx, memory_layout, None);
func.instruction(&Instruction::Drop);
func.instruction(&Instruction::I32Const(0));
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod compiler;
pub mod core;
pub mod ir;
pub mod optimize;
pub mod stdlib;
pub mod utils;

// WASM plugin integration
Expand Down
33 changes: 33 additions & 0 deletions src/stdlib/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
pub mod sys;

pub fn is_stdlib_module(name: &str) -> bool {
matches!(
name,
"sys"
| "os"
| "math"
| "random"
| "json"
| "re"
| "datetime"
| "collections"
| "itertools"
| "functools"
)
}

pub fn get_stdlib_attributes(module: &str, attr: &str) -> Option<StdlibValue> {
match module {
"sys" => sys::get_attribute(attr),
_ => None,
}
}

#[derive(Debug, Clone)]
pub enum StdlibValue {
Int(i32),
String(String),
List(Vec<String>),
Float(f64),
None,
}
13 changes: 13 additions & 0 deletions src/stdlib/sys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use crate::stdlib::StdlibValue;

pub fn get_attribute(attr: &str) -> Option<StdlibValue> {
match attr {
"argv" => Some(StdlibValue::List(vec![])),
"platform" => Some(StdlibValue::String("wasm32".to_string())),
"version" => Some(StdlibValue::String("3.11.0 (waspy)".to_string())),
"maxsize" => Some(StdlibValue::Int(i32::MAX)),
"stdin" | "stdout" | "stderr" => Some(StdlibValue::None),
"path" => Some(StdlibValue::List(vec![])),
_ => None,
}
}