Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I took some typical code from vm.nim, to show what kind of repetitive coding is still widely
- common practice (not just in the case of Nim):
- of opcAddFloat:
- decodeBC(rkFloat)
- regs[ra].floatVal = regs[rb].floatVal + regs[rc].floatVal
- of opcSubFloat:
- decodeBC(rkFloat)
- regs[ra].floatVal = regs[rb].floatVal - regs[rc].floatVal
- of opcMulFloat:
- decodeBC(rkFloat)
- regs[ra].floatVal = regs[rb].floatVal * regs[rc].floatVal
- of opcDivFloat:
- decodeBC(rkFloat)
- regs[ra].floatVal = regs[rb].floatVal / regs[rc].floatVal
- Here is pseudo code that would prevent this:
- oftemplate opc<when>Float(op: untyped):
- when Add: `+`
- when Sub: `-`
- when Mul: `*`
- when Div: `/`
- decodeBC(rkFloat)
- regs[ra].floatVal = op(regs[rb].floatVal, regs[rc].floatVal)
- 1) Using "oftemplate" to differentiate from normal of, but could in the end merge with normal "of" I think, if something like that became popular enough
- 2) I'm not sure on the <when> tag-like-syntax, maybe something better can be found
- 3) This is just an idea, please do not get offended by an idea!
- 4) This can already mostly be implemented with a regular Nim template. The advantage here is that the template gets cleanly embedded into the regular case-of structure. You would need this template only once anyways, so way create it outside of the case-of structure?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement