-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for globals. #90
Conversation
This PR is unfinished and is only "fully" implemented for the |
|
How do you think we should do it? |
I think you should make some test files that show what happens in other SOMs when you assign to a global, and then see how such behaviour might best be implemented in yksom. |
By assigning globals do you mean what was discussed here? #82 (comment) |
Yes, but there might be other ways of doing the same thing too (e.g. assigning to the variable |
Great I'll start that now and will let you know how I get on. |
@ummarikar please try all the things you can come up with, especially the things that should not work... I am, well, not entirely sure which cases are handled correctly... Via |
Will do! Thanks |
Working on |
I think we really need to see "hard" examples (as @smarr suggested) before critiquing the code, as the precise SOM semantics could change what we perceive to be the best route. |
Yes apologies, I’ve been working on something substantial but have hit a couple of roadblocks along the way. Will submit something either today or tomorrow. |
Still struggling to get this right don’t want to ask for help yet though so I will continue to try |
@smarr what are the intended semantics for |
Ignore me I had a look at the code for |
@ummarikar hm, I think this is a good question. See for instance: I would assume that returning the value is the most useful thing to do here, |
@smarr I agree! I’ll have |
This is my |
Just added the classes of som to |
Before I look into this in detail, can I check that you've run all the new tests on another SOM (e.g. Java SOM) to check that they behave exactly the same? |
I tried this in SOM but I got an error that |
Okay now that @smarr kindly, pointed out that I have been mistaking |
I am not sure why the boolean is necessary. And I do not understand the logic where the boolean is tested. In other SOM implementations, this boolean doesn't seem to be necessary. Metaclasses are also just instances of
Again, your PR is growing :) So, perhaps this is fine for the moment, but probably wants to be looked at at a later time again. |
I agree with @smarr that we need to get this PR to a resolution. It's partly my fault for asking questions. @ummarikar I suggest splitting this PR into two. Let's make this PR everything before the "adding metaclass" commit. You can do something like |
3c1c839
to
4e538bc
Compare
Done |
false | ||
" | ||
|
||
system_global = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To double check: does this test run identically on another SOM?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything except the last three lines but per @smarr 's request we kept nil
, true
and false
as non-globals.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, got it.
src/lib/vm/core.rs
Outdated
|
||
let val = self.compile(&path, inst_vars_allowed); | ||
let idx = self.add_symbol(name.to_string()); | ||
let globals = unsafe { &mut *self.globals.get() }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As elsewhere, we can't (generally) safely assign UnsafeCell
s to a variable, so unless we really need to, let's not do so.
In this case, we can probably change the code below to something like: unsafe { &mut *self.globals.get() }.entry(&idx).and_modify(|e| *e = val)
(notice that I think the Inst::new
is still bogus, right?).
@@ -326,6 +343,15 @@ impl VM { | |||
unsafe { &mut *self.stack.get() }.push(Double::new(self, i)); | |||
pc += 1; | |||
} | |||
Instr::Global(symbol_off) => { | |||
debug_assert!(unsafe { &*self.symbols.get() }.len() > symbol_off); | |||
if let Some(global) = unsafe { &mut *self.globals.get() }.get(&symbol_off) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can probably do the entry
trick here too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, no, ignore that comment -- sorry!
src/lib/vm/core.rs
Outdated
|
||
if let Some(i) = unsafe { &mut *self.reverse_symbols.get() }.get(s) { | ||
unsafe { &mut *self.globals.get() }.insert(*i, value.clone()); | ||
unsafe { &mut *self.stack.get() }.push(value.clone()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My guess is that the second clone
is unnecessary, but I might be wrong.
Done. |
let idx = self.add_symbol(name.to_string()); | ||
unsafe { &mut *self.globals.get() } | ||
.entry(idx) | ||
.or_insert(val.clone()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and_modify
did not pass the tests but this does.
src/lib/compiler/ast_to_instrs.rs
Outdated
match lex_string { | ||
"nil" => vm.instrs_push(Instr::Builtin(Builtin::Nil)), | ||
"false" => vm.instrs_push(Instr::Builtin(Builtin::False)), | ||
"system" => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this clause unnecessary in the sense that it's covered by the _
clause below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, must have forgotten to remove it. Done now
Please squash. |
f86e586
to
66c8704
Compare
@@ -44,6 +44,8 @@ pub enum VMError { | |||
expected: ObjType, | |||
got: ObjType, | |||
}, | |||
/// Tried to access a global before it being initialised. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment that was here before was incorrect so I changed it to this. Everything is squashed now!
Should these 3 commits be squashed down into 1? The last commit seems to fix fairly big problems in the first 1 or 2 commits, so I'm not sure those are very useful to keep in the history. However, I can be convinced otherwise. |
You make a very good point I'll squash this into one. |
66c8704
to
db065ec
Compare
Squashed. |
Would I be right in thinking that the commit title should really be "add support for globals"? |
Yes you would, one second. |
Add a hashmap which maps symbols to their corresponding object and retrieve an object from this hashmap when its symbol is referenced during runtime.
db065ec
to
3c0ce78
Compare
Done :) |
bors r+ |
90: Add support for globals. r=ltratt a=ummarikar Add a hashmap which maps symbols to their corresponding object and retrieve an object from this hashmap when its symbol is referenced during runtime. Co-authored-by: Umar Marikar <[email protected]>
Build succeeded |
Add a hashmap which maps symbols to their corresponding object and retrieve an object from this hashmap when its symbol is referenced during runtime.