Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. I took some typical code from vm.nim, to show what kind of repetitive coding is still widely
  2. common practice (not just in the case of Nim):
  3.  
  4. of opcAddFloat:
  5. decodeBC(rkFloat)
  6. regs[ra].floatVal = regs[rb].floatVal + regs[rc].floatVal
  7. of opcSubFloat:
  8. decodeBC(rkFloat)
  9. regs[ra].floatVal = regs[rb].floatVal - regs[rc].floatVal
  10. of opcMulFloat:
  11. decodeBC(rkFloat)
  12. regs[ra].floatVal = regs[rb].floatVal * regs[rc].floatVal
  13. of opcDivFloat:
  14. decodeBC(rkFloat)
  15. regs[ra].floatVal = regs[rb].floatVal / regs[rc].floatVal
  16.  
  17. Here is pseudo code that would prevent this:
  18. oftemplate opc<when>Float(op: untyped):
  19. when Add: `+`
  20. when Sub: `-`
  21. when Mul: `*`
  22. when Div: `/`
  23. decodeBC(rkFloat)
  24. regs[ra].floatVal = op(regs[rb].floatVal, regs[rc].floatVal)
  25.  
  26. 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
  27. 2) I'm not sure on the <when> tag-like-syntax, maybe something better can be found
  28. 3) This is just an idea, please do not get offended by an idea!
  29. 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