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
2 changes: 1 addition & 1 deletion interpreter/builtin.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ let abort_fn : RuntimeFunction = ctx => {
/// 布尔 NOT 运算
let bool_not_fn : RuntimeFunction = ctx => {
if ctx.args is [{ val: Bool(b), .. }] {
Bool(not(b))
Bool(!b)
} else {
Unit
}
Expand Down
4 changes: 2 additions & 2 deletions interpreter/control_flow.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn ClosureInterpreter::visit_for(
}
None => true
}
if not(should_continue) {
if !should_continue {
break
}

Expand Down Expand Up @@ -107,7 +107,7 @@ fn ClosureInterpreter::visit_while(
|> ignore
}
// 如果没有break,执行else分支
if not(broke) {
if !broke {
match while_else {
Some(else_expr) =>
result = self.visit_scoped(
Expand Down
86,391 changes: 46,195 additions & 40,196 deletions interpreter/core_modules.mbt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion interpreter/env.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn RuntimeEnvironment::create_closure_env(
// 只捕获不可变变量的值,可变变量通过父环境引用
self.values.each(fn(key, value) {
let is_mutable = self.mutable_vars.get(key).unwrap_or(false)
if not(is_mutable) {
if !is_mutable {
// 不可变变量:捕获当前值
values.set(key, value)
mutable_vars.set(key, false)
Expand Down
31 changes: 31 additions & 0 deletions interpreter/expression_visitors.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,25 @@ fn ClosureInterpreter::visit_array(
Array(final_array)
}

///|
fn ClosureInterpreter::call_struct_constr(
self : ClosureInterpreter,
pkg_name : String?,
type_name : String,
field_values : @list.List[@syntax.Argument],
) -> RuntimeValue? raise ControlFlow {
let pkg = match pkg_name {
Some(name) => self.find_pkg(name)
None => self.current_pkg
}
let ty = pkg.find_static_type(type_name)
if pkg.struct_constrs.get(type_name) is Some(constr_name) {
self.execute_static_method_call(ty, constr_name, field_values) |> Some
} else {
None
}
}

///|
/// 处理函数调用
fn ClosureInterpreter::visit_apply(
Expand All @@ -161,6 +180,18 @@ fn ClosureInterpreter::visit_apply(
Ident(id={ name, .. }, ..) => self.call_by_name(name, args)
// 处理构造函数调用,如 Some(5)
Constr(constr~, ..) => {
match constr.extra_info {
TypeName(_) => ()
Package(pkg_name) if self.call_struct_constr(
Some(pkg_name),
constr.name.name,
args,
)
is Some(result) => return result
NoExtraInfo if self.call_struct_constr(None, constr.name.name, args)
is Some(result) => return result
_ => ()
}
let fields = args
.map(arg => {
match arg.kind {
Expand Down
1 change: 1 addition & 0 deletions interpreter/fs.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub let fs_package : RuntimePackage = {
trait_aliases: Map::new(),
fn_aliases: Map::new(),
trait_methods: Map::new(),
struct_constrs: Map::new(),
values: fs_methods.map((_, v) => Fn({ val: v, ty: Internal })),
env,
deps: Map::new(),
Expand Down
2 changes: 1 addition & 1 deletion interpreter/hashmap.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ let hash_map_to_string_fn : RuntimeFunction = ctx => {
sb.write_string("{")
let mut first = true
map.each(fn(key, val) {
if not(first) {
if !first {
sb.write_string(", ")
}
first = false
Expand Down
9 changes: 8 additions & 1 deletion interpreter/interpreter.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn ClosureInterpreter::add_embedded_method(
method_name : String,
f : RuntimeFunction,
) -> Unit {
if not(self.embedded_methods.contains(type_name)) {
if !self.embedded_methods.contains(type_name) {
self.embedded_methods.set(type_name, Map::new())
}
self.embedded_methods.get(type_name).unwrap().set(method_name, f)
Expand Down Expand Up @@ -271,6 +271,12 @@ pub fn ClosureInterpreter::top_visit(
TopTypeDef(def) => {
match def {
{ tycon, params, components, deriving, .. } => {
if def.components is Record(constr_decl=Some(constr_decl), ..) {
self.current_pkg.struct_constrs.set(
def.tycon,
constr_decl.name.name,
)
}
self.current_pkg.type_definitions.set(tycon, def)

// 处理泛型类型参数
Expand Down Expand Up @@ -402,6 +408,7 @@ pub fn ClosureInterpreter::top_visit(
}
_ => ()
}
DeclNone => ()
}
Unit
}
Expand Down
13 changes: 5 additions & 8 deletions interpreter/iter2.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,21 @@ pub let iter2_methods : Map[String, RuntimeFunction] = {
let iter2_run_fn : RuntimeFunction = ctx => {
match ctx.args {
[{ val: Iter2(iter2), .. }, { val: Fn(func), .. }] => {
let result = iter2.run(fn(a, b) {
for a, b in iter2 {
try {
match
ctx.context.call(func.val, ctx.pkg, [
{ val: a, kind: Positional },
{ val: b, kind: Positional },
]) {
Unit => IterEnd
_ => IterContinue
Unit => continue
_ => break
}
} catch {
_ => IterEnd
_ => break
}
})
match result {
IterEnd => Unit
IterContinue => Unit
}
Unit
}
_ => Unit
}
Expand Down
29 changes: 25 additions & 4 deletions interpreter/module.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,41 @@ pub fn ModuleInfo::get_zip_url(self : ModuleInfo) -> String noraise {
"\{url_base}/\{self.name}\{v}.zip"
}

///|
/// Wrapper around @syntax.TraitDecl with placeholder ToJson implementation.
struct TraitDecl(@syntax.TraitDecl)

///|
impl ToJson for TraitDecl with to_json(self) {
{ "name": self.0.name.name }
}

///|
/// Wrapper around @syntax.TypeDecl with placeholder ToJson implementation
/// and optional constructor information introduced since 0.8.0.
struct TypeDecl(@syntax.TypeDecl)

///|
impl ToJson for TypeDecl with to_json(self) {
{ "name": self.0.tycon }
}

///|
pub(all) struct RuntimePackage {
name : String
traits : Map[String, @syntax.TraitDecl]
traits : Map[String, TraitDecl]
// 函数别名存储 - 存储函数名到其实际实现的映射
fn_aliases : Map[String, RuntimeValue]
type_aliases : Map[String, WithType[@syntax.TypeDecl]]
type_aliases : Map[String, WithType[TypeDecl]]
// trait 别名存储 - 存储 trait 名到其实际实现的映射
trait_aliases : Map[String, WithType[@syntax.TraitDecl]]
trait_aliases : Map[String, WithType[TraitDecl]]
// 存根 - 存储函数名到其实际实现的映射
stubs : Map[String, String]
// 类型定义
type_definitions : Map[String, @syntax.TypeDecl]
type_definitions : Map[String, TypeDecl]
// 类型派生的trait映射 - 存储类型名到其derive的trait列表
type_derived_traits : Map[String, Array[String]] // type_name -> [trait_name]
struct_constrs : Map[String, String]
// 构造函数集合 - 存储构造函数名
constructors : Map[String, String] // constructor_name -> type_name
struct_methods : Map[String, Map[String, RuntimeValue]]
Expand Down Expand Up @@ -104,6 +124,7 @@ pub fn RuntimePackage::new(
deps: deps.unwrap_or(Map::new()),
files: files.unwrap_or(Map::new()),
loaded: false,
struct_constrs: Map::new(),
}
}

Expand Down
8 changes: 4 additions & 4 deletions interpreter/pattern_matching.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub fn ClosureInterpreter::match_case(
while true {
match pat_list {
@list.More(pat, tail=pat_tail) => {
if not(self.match_case(values[index], pat)) {
if !self.match_case(values[index], pat) {
return false
}
pat_list = pat_tail
Expand Down Expand Up @@ -245,7 +245,7 @@ pub fn ClosureInterpreter::match_case(
@list.More(array_pat, tail=pat_tail) => {
match array_pat {
@syntax.ArrayPattern::Pattern(pat) =>
if not(self.match_case(values[index], pat)) {
if !self.match_case(values[index], pat) {
return false
}
_ => return false // 其他数组模式暂不支持
Expand Down Expand Up @@ -273,7 +273,7 @@ pub fn ClosureInterpreter::match_case(
for pat_field in pat_fields {
match record_fields.get(pat_field.label.name) {
Some(field_value) =>
if not(self.match_case(field_value, pat_field.pattern)) {
if !self.match_case(field_value, pat_field.pattern) {
return false
}
None => return false
Expand Down Expand Up @@ -307,7 +307,7 @@ pub fn ClosureInterpreter::match_case(
@list.More(pat_arg, tail=pat_tail) => {
// 提取参数模式并递归匹配
let arg_pattern = pat_arg.pat
if not(self.match_case(fields[index].value, arg_pattern)) {
if !self.match_case(fields[index].value, arg_pattern) {
return false
}
match pat_arg.kind {
Expand Down
38 changes: 16 additions & 22 deletions interpreter/pkg.generated.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,9 @@ pub(all) struct ModuleInfo {
keywords : Array[String]?
description : String?
source : String?
}
} derive(Show, ToJson, @json.FromJson)
pub fn ModuleInfo::get_zip_url(Self) -> String
pub fn ModuleInfo::new(String, version? : String, readme? : String, repository? : String, license? : String, keywords? : Array[String], description? : String, source? : String, deps? : Map[String, String]) -> Self
pub impl Show for ModuleInfo
pub impl ToJson for ModuleInfo
pub impl @json.FromJson for ModuleInfo

pub(all) struct RuntimeArgument {
val : RuntimeValue
Expand All @@ -157,26 +154,24 @@ pub(all) struct RuntimeEnvironment {
values : Map[String, RuntimeValue]
mutable_vars : Map[String, Bool]
parent : RuntimeEnvironment?
}
} derive(ToJson)
pub fn RuntimeEnvironment::copy(Self) -> Self
pub fn RuntimeEnvironment::create_closure_env(Self) -> Self
pub fn RuntimeEnvironment::find(Self, String) -> RuntimeValue?
pub fn RuntimeEnvironment::new(parent? : Self, values? : Map[String, RuntimeValue]) -> Self
pub fn RuntimeEnvironment::set(Self, String, RuntimeValue) -> Unit
pub fn RuntimeEnvironment::set_mutable_variable(Self, String, RuntimeValue) -> Unit
pub fn RuntimeEnvironment::update(Self, String, RuntimeValue) -> Unit
pub impl ToJson for RuntimeEnvironment

pub(all) enum RuntimeErrorType {
ErrorType(RuntimeType)
DefaultErrorType
NoErrorType
Noraise
MaybeError(RuntimeType)
}
} derive(ToJson)
pub impl Eq for RuntimeErrorType
pub impl Show for RuntimeErrorType
pub impl ToJson for RuntimeErrorType

pub(all) struct RuntimeFunctionContext {
context : ClosureInterpreter
Expand All @@ -189,23 +184,21 @@ pub enum RuntimeLocation {
FunctionCall(String)
ControlFlow(String)
LetMut(String)
}
pub impl Show for RuntimeLocation
} derive(Show)

pub(all) struct RuntimeModule {
meta : ModuleInfo
pkgs : Map[String, RuntimePackage]
}
pub impl ToJson for RuntimeModule
} derive(ToJson)

pub(all) struct RuntimePackage {
name : String
traits : Map[String, @syntax.TraitDecl]
traits : Map[String, TraitDecl]
fn_aliases : Map[String, RuntimeValue]
type_aliases : Map[String, WithType[@syntax.TypeDecl]]
trait_aliases : Map[String, WithType[@syntax.TraitDecl]]
type_aliases : Map[String, WithType[TypeDecl]]
trait_aliases : Map[String, WithType[TraitDecl]]
stubs : Map[String, String]
type_definitions : Map[String, @syntax.TypeDecl]
type_definitions : Map[String, TypeDecl]
type_derived_traits : Map[String, Array[String]]
constructors : Map[String, String]
struct_methods : Map[String, Map[String, RuntimeValue]]
Expand All @@ -215,7 +208,7 @@ pub(all) struct RuntimePackage {
deps : Map[String, RuntimePackage]
files : Map[String, String]
mut loaded : Bool
}
} derive(ToJson)
pub fn RuntimePackage::check_type_constraint(Self, RuntimeValue, @syntax.Type) -> Bool
pub fn RuntimePackage::cons(Self, String, Array[RuntimeValue]) -> RuntimeValue
pub fn RuntimePackage::cons_with_labels(Self, String, Array[(String?, RuntimeValue, Bool)]) -> RuntimeValue
Expand All @@ -225,7 +218,6 @@ pub fn RuntimePackage::find_stub(Self, String) -> String
pub fn RuntimePackage::is_constructor(Self, String) -> Bool
pub fn RuntimePackage::new(String, deps? : Map[String, Self], files? : Map[String, String]) -> Self
pub fn RuntimePackage::set(Self, String, RuntimeValue) -> Unit
pub impl ToJson for RuntimePackage

pub(all) enum RuntimeType {
Any
Expand All @@ -235,7 +227,7 @@ pub(all) enum RuntimeType {
Option(RuntimeType)
Object(pkg~ : RuntimePackage, name~ : String)
Internal
}
} derive(ToJson)
pub fn RuntimeType::any() -> Self
pub fn RuntimeType::array() -> Self
pub fn RuntimeType::array_view() -> Self
Expand Down Expand Up @@ -266,7 +258,6 @@ pub fn RuntimeType::uint64() -> Self
pub fn RuntimeType::uninitialized_array() -> Self
pub impl Eq for RuntimeType
pub impl Show for RuntimeType
pub impl ToJson for RuntimeType

pub(all) enum RuntimeValue {
Unit
Expand Down Expand Up @@ -316,11 +307,14 @@ pub impl Hash for RuntimeValue
pub impl Show for RuntimeValue
pub impl ToJson for RuntimeValue

type TraitDecl

type TypeDecl

pub struct WithType[T] {
val : T
ty : RuntimeType
}
pub impl[T : ToJson] ToJson for WithType[T]
} derive(ToJson)

// Type aliases
pub type RuntimeFunction = (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow
Expand Down
2 changes: 1 addition & 1 deletion interpreter/runtime_value.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ fn compare_types(
while true {
match (tys_a, tys_b) {
(@list.More(ty_a, tail=tail_a), @list.More(ty_b, tail=tail_b)) => {
if not(compare_type_signatures(ty_a, ty_b)) {
if !compare_type_signatures(ty_a, ty_b) {
return false
}
tys_a = tail_a
Expand Down
2 changes: 1 addition & 1 deletion interpreter/type_env.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ fn ClosureInterpreter::parse_function_type(

///|
pub fn RuntimeType::bool() -> RuntimeType {
Object(pkg=moonbitlang_core_bool_module, name="Bool")
Object(pkg=moonbitlang_core_builtin_module, name="Bool")
}

///|
Expand Down
2 changes: 1 addition & 1 deletion interpreter/type_operations.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn RuntimePackage::infer_struct_type_from_fields(
.fold(init="Any", fn(result, entry) {
let (type_name, type_def) = entry
match type_def.components {
Record(struct_fields) => {
Record(fields=struct_fields, ..) => {
let all_matched = struct_fields.all(sf => {
current_fields.iter().any(cf => sf.name.label == cf)
})
Expand Down
Loading
Loading