Skip to content

Commit 5fdc800

Browse files
authored
Merge pull request #2 from gilascale/feature/assignment-semantics
feature: assignment semantics
2 parents 7c5f131 + 9dea165 commit 5fdc800

File tree

4 files changed

+67
-30
lines changed

4 files changed

+67
-30
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ v.display()
5858

5959
### Bugs
6060

61+
- fix the define vs assign issue at the moment, at the moment they are the same
62+
thing
6163
- creating 2 long lists causes the first list to be an element of the second
6264
list?
6365
- fix tuple parsing...
@@ -95,12 +97,8 @@ v.display()
9597

9698
### Language Features
9799

98-
- garbage collection
99-
- bug is with the compilation of prelude included, we are executing the
100-
prelude twice
101-
- i think a fix for this is changing how its loaded up... we need to do an
102-
'import' of the prelude into the global scope
103-
- ranges can be parsed anywhere
100+
- setting a value outside a scope
101+
- i.e. iterating a variable in a function doesn't work
104102
- implicit returns
105103
- void types
106104
- tuple unpacking

example/test.gila

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,11 @@
22

33

44

5+
l1 = [1,3,5,7,9]
56

6-
l1 = [3,4,2,1,3,3]
7+
x = 0
78

8-
l2 = [4,3,5,3,9,3]
9-
10-
11-
12-
13-
s = SliceIterator(counter=0,s=l1)
14-
15-
print(s)
16-
17-
18-
for _ in s
19-
print("foo")
9+
for _ in 0..len(l1) do
10+
print(l1[x])
11+
x = x + 1
12+
end

src/codegen.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,20 +1332,18 @@ impl BytecodeGenerator {
13321332
panic!()
13331333
}
13341334

1335-
// todo we need a map or something to map these to registers
1336-
fn gen_variable(
1335+
fn lookup_variable_recursively(
13371336
&mut self,
13381337
annotation_context: AnnotationContext,
1339-
pos: Position,
1338+
pos: &Position,
13401339
t: &Token,
1341-
) -> u8 {
1342-
// todo we assume it exists so return the map
1340+
) -> Option<u8> {
13431341
let result = self.codegen_context.chunks[self.codegen_context.current_chunk_pointer]
13441342
.variable_map
13451343
.get(&t.as_identifier());
13461344

13471345
if let Some(v) = result {
1348-
return *v;
1346+
return Some(*v);
13491347
} else {
13501348
let reg = alloc_slot!(self);
13511349
let mut counter = self.codegen_context.current_chunk_pointer;
@@ -1363,16 +1361,29 @@ impl BytecodeGenerator {
13631361
},
13641362
pos.line as usize,
13651363
);
1366-
return reg;
1364+
return Some(reg);
13671365
}
13681366
if counter == 0 {
1369-
break;
1367+
return None;
13701368
}
13711369
counter -= 1;
13721370
}
13731371
}
1372+
}
13741373

1375-
panic!("variable not found in any scope {:?}", t)
1374+
// todo we need a map or something to map these to registers
1375+
fn gen_variable(
1376+
&mut self,
1377+
annotation_context: AnnotationContext,
1378+
pos: Position,
1379+
t: &Token,
1380+
) -> u8 {
1381+
// todo we assume it exists so return the map
1382+
let res = self.lookup_variable_recursively(annotation_context, &pos, t);
1383+
if res.is_some() {
1384+
return res.unwrap();
1385+
}
1386+
panic!();
13761387
}
13771388

13781389
fn get_variable(
@@ -1404,9 +1415,14 @@ impl BytecodeGenerator {
14041415

14051416
match value {
14061417
Some(v) => {
1407-
let location = self.visit(annotation_context, &v);
1418+
let location = self.visit(annotation_context.clone(), &v);
14081419

1409-
let var_location = alloc_perm_slot!(self);
1420+
let existing_var = self.lookup_variable_recursively(annotation_context, &pos, var);
1421+
let var_location = if existing_var.is_some() {
1422+
existing_var.unwrap()
1423+
} else {
1424+
alloc_perm_slot!(self)
1425+
};
14101426

14111427
self.codegen_context.chunks[self.codegen_context.current_chunk_pointer]
14121428
.variable_map

src/execution.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,36 @@ impl Object {
640640
}
641641
_ => return Ok(false),
642642
},
643+
GCRefData::SLICE(s) => match other {
644+
Object::GC_REF(other_gc_ref) => {
645+
let other_res = shared_execution_context.heap.deref(&other_gc_ref);
646+
if other_res.is_err() {
647+
return Err(other_res.err().unwrap());
648+
}
649+
match other_res.unwrap() {
650+
GCRefData::SLICE(other_s) => {
651+
if s.s.len() != other_s.s.len() {
652+
return Ok(false);
653+
}
654+
let mut i = 0;
655+
for item in s.s {
656+
let eq_res = item
657+
.equals(shared_execution_context, other_s.s[i].clone());
658+
if eq_res.is_err() {
659+
return Err(eq_res.err().unwrap());
660+
}
661+
if eq_res.unwrap() {
662+
return Ok(false);
663+
}
664+
i += 1;
665+
}
666+
return Ok(true);
667+
}
668+
_ => return Ok(false),
669+
}
670+
}
671+
_ => return Ok(false),
672+
},
643673
_ => todo!(),
644674
}
645675
Ok(true)

0 commit comments

Comments
 (0)