Skip to content

Commit 46d21cc

Browse files
committed
fix initial bug, expose error with self variable not being captured
1 parent 1aba9d7 commit 46d21cc

10 files changed

Lines changed: 214 additions & 16 deletions

File tree

compiler/src/ast/assignment.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ impl Assignment {
127127

128128
impl Dependencies for Assignment {
129129
fn supplies(&self) -> Vec<Dependency> {
130-
if self.idents.len() == 1 && !self.flags().contains(AssignmentFlag::modify()) {
131-
return vec![Dependency::new(Cow::Borrowed(&self.idents[0]))];
130+
if !self.flags().contains(AssignmentFlag::modify()) {
131+
return self.idents.iter().map(|ident| Dependency::new(Cow::Borrowed(ident))).collect();
132132
}
133133

134134
// We are not introducing a new variable, just pointing to a callback variable.

compiler/src/ast/class/member_function.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,28 @@ use bytecode::compilation_bridge::id::{MAKE_FUNCTION, RET};
66
use crate::{
77
ast::{
88
function::FunctionType, Block, CompilationState, Compile, CompiledFunctionId, CompiledItem,
9-
Dependencies, FunctionParameters, Ident, TypeLayout, WalkForType,
9+
Dependencies, Dependency, FunctionParameters, Ident, TypeLayout, WalkForType,
1010
},
1111
instruction,
1212
parser::{Node, Parser, Rule},
1313
scope::ScopeReturnStatus,
1414
BytecodePathStr, VecErr,
1515
};
1616

17+
use super::ClassType;
18+
1719
#[derive(Debug)]
1820
pub(crate) struct MemberFunction {
1921
ident: Ident,
2022
parameters: Rc<FunctionParameters>,
2123
body: Block,
2224
path_str: Arc<PathBuf>,
23-
class_name: Arc<String>,
25+
class_type: ClassType,
2426
}
2527

2628
impl MemberFunction {
2729
pub fn symbolic_id(&self) -> String {
28-
format!("{}::{}", self.class_name, self.ident().name())
30+
format!("{}::{}", self.class_type.name(), self.ident().name())
2931
}
3032
}
3133

@@ -117,7 +119,14 @@ impl Dependencies for MemberFunction {
117119
}
118120

119121
fn supplies(&self) -> Vec<crate::ast::Dependency> {
120-
self.parameters.supplies()
122+
let mut params = self.parameters.supplies();
123+
params.push(Dependency::new(Cow::Owned(Ident::new(
124+
"self".to_owned(),
125+
Some(Cow::Owned(TypeLayout::Class(self.class_type.clone()))),
126+
false,
127+
))));
128+
129+
params
121130
}
122131
}
123132

@@ -162,7 +171,7 @@ impl Parser {
162171
parameters,
163172
path_str: input.user_data().bytecode_path(),
164173
body,
165-
class_name: class_type.arced_name(),
174+
class_type,
166175
})
167176
}
168177
}

compiler/src/ast/function_body.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl Dependencies for Block {
2323
fn dependencies(&self) -> Vec<Dependency> {
2424
let block_dependencies = self.0.iter().flat_map(|x| x.net_dependencies()).collect();
2525

26-
block_dependencies
26+
dbg!(block_dependencies)
2727
}
2828

2929
fn net_dependencies(&self) -> Vec<Dependency> {

compiler/src/ast/math_expr.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use pest::{
1414
};
1515

1616
use crate::{
17-
ast::{number, r#type::TypecheckFlags, Callable, ConstexprEvaluation},
17+
ast::{number, r#type::TypecheckFlags, Callable, ConstexprEvaluation, Ident},
1818
instruction,
1919
parser::{AssocFileData, Node, Parser, Rule},
2020
CompilationError, VecErr,
@@ -154,7 +154,7 @@ pub(crate) enum CallableContents {
154154
pub(crate) trait UnwrapSpanDisplayable: Debug + Display {}
155155
impl<T> UnwrapSpanDisplayable for T where T: Debug + Display {}
156156

157-
#[derive(Debug)]
157+
#[derive(Debug, Clone)]
158158
pub enum ReferenceToSelf {
159159
Class(ClassType),
160160
Function,
@@ -444,8 +444,28 @@ impl Dependencies for Expr {
444444
lhs_deps.append(&mut index.net_dependencies());
445445
lhs_deps
446446
}
447-
E::DotLookup { lhs, .. } => lhs.net_dependencies(),
448-
E::ReferenceToSelf { .. } => vec![],
447+
E::DotLookup { lhs, .. } => {
448+
let x = lhs.net_dependencies();
449+
450+
println!("{x:?}");
451+
452+
x
453+
}
454+
E::ReferenceToSelf(reference_to_self) => {
455+
let reference_to_self = match reference_to_self.borrow().clone() {
456+
ReferenceToSelf::Class(class) => TypeLayout::Class(class.clone()),
457+
_ => {
458+
log::warn!("Invalid reference to self");
459+
return vec![];
460+
}
461+
};
462+
let self_dependency = Dependency::new(Cow::Owned(Ident::new(
463+
"self".to_owned(),
464+
Some(Cow::Owned(reference_to_self)),
465+
false,
466+
)));
467+
vec![self_dependency]
468+
}
449469
E::ReferenceToConstructor(..) => vec![],
450470
E::Nil => vec![],
451471
E::UnaryUnwrap { value, .. } => value.net_dependencies(),

compiler/src/ast/print_statement.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct PrintStatement(Value);
1212

1313
impl Dependencies for PrintStatement {
1414
fn dependencies(&self) -> Vec<Dependency> {
15-
self.0.net_dependencies()
15+
dbg!(self.0.net_dependencies())
1616
}
1717
}
1818

compiler/src/ast/value.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,20 @@ impl Value {
195195

196196
impl Dependencies for Value {
197197
fn dependencies(&self) -> Vec<Dependency> {
198-
match self {
198+
let x = match self {
199199
Self::Function(function) => function.net_dependencies(),
200-
Self::Ident(name) => name.net_dependencies(),
200+
Self::Ident(name) => dbg!(name.net_dependencies()),
201201
Self::Number(number) => number.net_dependencies(),
202202
Self::String(string) => string.net_dependencies(),
203203
Self::MathExpr(math_expr) => math_expr.net_dependencies(),
204204
Self::Boolean(boolean) => boolean.net_dependencies(),
205205
Self::List(list) => list.net_dependencies(),
206206
Self::Map(map) => map.net_dependencies(),
207-
}
207+
};
208+
209+
dbg!(self);
210+
211+
dbg!(x)
208212
}
209213
}
210214

examples/crashes/#197.mmm

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
function __fn0
2+
load_fast "self"
3+
printn "*"
4+
void
5+
void
6+
ret
7+
end
8+
function Foo::bar
9+
arg "0"
10+
store "self"
11+
make_function "./examples/crashes/#197.mmm#__fn0" "self"
12+
ret
13+
end
14+
function Foo::$constructor
15+
arg "0"
16+
store "self"
17+
void
18+
ret
19+
end
20+
function Foo
21+
make_function "./examples/crashes/#197.mmm#Foo::bar"
22+
store_fast "Foo::bar"
23+
make_object
24+
store_fast "#1"
25+
make_function "./examples/crashes/#197.mmm#Foo::$constructor"
26+
store_fast "#0"
27+
load_fast "#1"
28+
load_fast "#0"
29+
call
30+
load_fast "#1"
31+
ret
32+
end
33+
function __module__
34+
make_function "./examples/crashes/#197.mmm#Foo"
35+
export_special "Foo" "Foo"
36+
load "Foo"
37+
store_fast "#1"
38+
load_fast "#1"
39+
call
40+
store "foo"
41+
load "foo"
42+
store_fast "#1"
43+
load_fast "#1"
44+
lookup "bar"
45+
store_fast "#2"
46+
ld_self "#1"
47+
load_fast "#2"
48+
call
49+
store "x"
50+
load "x"
51+
store_fast "#1"
52+
load_fast "#1"
53+
call
54+
void
55+
ret_mod
56+
end

examples/crashes/#197.ms

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
###
2+
class ConditionFactory {
3+
valid_inputs: [[int, str]...]
4+
5+
constructor(self) {
6+
self.valid_inputs = []
7+
}
8+
9+
fn with_input(self, number: int, repr: str) -> Self {
10+
self.valid_inputs.push([number, repr])
11+
return self
12+
}
13+
14+
fn len(self) -> int {
15+
return self.valid_inputs.len()
16+
}
17+
18+
fn nth(self, index: int) -> [int, str] {
19+
return (self.valid_inputs)[index]
20+
}
21+
}
22+
23+
class FizzBuzzFactory {
24+
conditions: [ConditionFactory...]
25+
26+
constructor(self) {
27+
self.conditions = []
28+
}
29+
30+
fn with_condition(self, condition_factory: ConditionFactory) -> Self {
31+
self.conditions.push(condition_factory)
32+
return self
33+
}
34+
35+
fn build(self) -> (fn(int) -> str) {
36+
return fn(input: int) -> str {
37+
result = ""
38+
from 0 to self.conditions.len(), i {
39+
conditions = (self.conditions)[i]
40+
41+
from 0 to conditions.len(), j {
42+
[number, repr] = conditions.nth(j)
43+
44+
if input % number == 0 {
45+
result += repr
46+
}
47+
}
48+
}
49+
return result
50+
}
51+
}
52+
}
53+
54+
fizzbuzz = (FizzBuzzFactory())
55+
.with_condition(
56+
(ConditionFactory())
57+
.with_input(3, "Fizz")
58+
)
59+
.with_condition(
60+
(ConditionFactory())
61+
.with_input(5, "Buzz")
62+
)
63+
.build()
64+
65+
print fizzbuzz(15)
66+
###
67+
68+
class Foo {
69+
fn bar(self) -> fn() {
70+
return fn() {
71+
print self
72+
}
73+
}
74+
}
75+
76+
foo = Foo()
77+
x = foo.bar()
78+
x()

examples/maps/leetcode_3.ms

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
length_of_longest_substring = fn(input: str) -> int {
2+
letter_frequency = map[str, bool]
3+
4+
start_index = 0
5+
6+
result = 0
7+
8+
from 0 to input.len(), i {
9+
if letter_frequency[input[i]] != nil {
10+
length = i - start_index
11+
12+
if length > result {
13+
result = length
14+
}
15+
16+
letter_frequency.clear()
17+
letter_frequency[input[i]] = true
18+
19+
start_index = i
20+
continue
21+
}
22+
23+
letter_frequency[input[i]] = true
24+
}
25+
26+
return result
27+
}
28+
29+
print length_of_longest_substring("pwwkew")
30+

rustup

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 5af4bc4a0d4bc69ea9091a7935fb3783c5fb508e

0 commit comments

Comments
 (0)