Skip to content

Commit 057f51b

Browse files
committed
refactor: ImlValue::compile()
1 parent 00baed1 commit 057f51b

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

src/compiler/iml/imlvalue.rs

+33-35
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,8 @@ impl ImlValue {
344344
ops.push(Op::Offset(Box::new(*offset)));
345345
}
346346

347-
// Remember current ops start
348-
let start = ops.len();
349-
350-
match self {
347+
// First, try to push some Op for the value
348+
let op = match self {
351349
Self::Shared(value) => {
352350
return value.borrow().compile(program, current, offset, call, ops)
353351
}
@@ -357,37 +355,47 @@ impl ImlValue {
357355
.unwrap()
358356
.compile(program, current, offset, call, ops)
359357
}
360-
Self::VoidToken => ops.push(Op::Next),
358+
Self::VoidToken => Some(Op::Next),
361359
Self::Value(value) => match &*value.borrow() {
362-
Value::Void => ops.push(Op::PushVoid),
363-
Value::Null => ops.push(Op::PushNull),
364-
Value::True => ops.push(Op::PushTrue),
365-
Value::False => ops.push(Op::PushFalse),
360+
Value::Void => Some(Op::PushVoid),
361+
Value::Null => Some(Op::PushNull),
362+
Value::True => Some(Op::PushTrue),
363+
Value::False => Some(Op::PushFalse),
366364
Value::Int(i) => match i.to_i32() {
367-
Some(0) => ops.push(Op::Push0),
368-
Some(1) => ops.push(Op::Push1),
369-
_ => { /* continue below! */ }
365+
Some(0) => Some(Op::Push0),
366+
Some(1) => Some(Op::Push1),
367+
_ => None,
370368
},
371-
_ => { /* continue below! */ }
369+
_ => None,
372370
},
373-
Self::Parselet(_) => { /* continue below! */ }
374371
Self::Variable {
375-
addr, is_global, ..
372+
is_global, addr, ..
376373
} => {
377374
if *is_global {
378-
ops.push(Op::LoadGlobal(*addr))
375+
Some(Op::LoadGlobal(*addr))
379376
} else {
380-
ops.push(Op::LoadFast(*addr))
377+
Some(Op::LoadFast(*addr))
381378
}
382379
}
383-
Self::Instance(_instance) => {
384-
todo!();
385-
}
386-
_ => unreachable!("{}", self),
387-
}
380+
_ => None,
381+
};
388382

389383
// Check if something has been pushed before.
390-
if start == ops.len() {
384+
if let Some(op) = op {
385+
ops.push(op); // Push the op
386+
387+
match call {
388+
// Load (already done previously)
389+
None => {}
390+
// Call or load
391+
Some(None) => ops.push(Op::CallOrCopy),
392+
// Call (qualified)
393+
Some(Some((0, false))) => ops.push(Op::Call),
394+
Some(Some((args, false))) => ops.push(Op::CallArg(args)),
395+
Some(Some((args, true))) => ops.push(Op::CallArgNamed(args)),
396+
}
397+
} else {
398+
// Register new static
391399
let idx = match self {
392400
ImlValue::Parselet(parselet) => match parselet.derive(current.0) {
393401
Ok(parselet) => program.register(&ImlValue::Parselet(parselet)),
@@ -396,7 +404,8 @@ impl ImlValue {
396404
return;
397405
}
398406
},
399-
resolved => program.register(resolved),
407+
ImlValue::Value(_) => program.register(self),
408+
_ => unreachable!("Can't compile {:?}", self),
400409
};
401410

402411
match call {
@@ -415,17 +424,6 @@ impl ImlValue {
415424
Some(Some((args, false))) => ops.push(Op::CallStaticArg(Box::new((idx, args)))),
416425
Some(Some((args, true))) => ops.push(Op::CallStaticArgNamed(Box::new((idx, args)))),
417426
}
418-
} else {
419-
match call {
420-
// Load (already done previously)
421-
None => {}
422-
// Call or load
423-
Some(None) => ops.push(Op::CallOrCopy),
424-
// Call (qualified)
425-
Some(Some((0, false))) => ops.push(Op::Call),
426-
Some(Some((args, false))) => ops.push(Op::CallArg(args)),
427-
Some(Some((args, true))) => ops.push(Op::CallArgNamed(args)),
428-
}
429427
}
430428
}
431429
}

0 commit comments

Comments
 (0)