Skip to content

9a) Extending cm

Benjamin Pang edited this page Nov 7, 2016 · 3 revisions

NOTE: Extending cm is rather risky and should only be used sparingly. js is an extremely powerful function that can potentially ruin the interpreter (or, even worse, your computer). Only do this if you know very well what you're doing - otherwise, sticking with pure DASH will keep things just fine.

Making new built-in functions is much easier with the js function. js executes within the context of the interpreter itself, so Javascript code executed by js has full access to interpreter functions.

To start, you should familiarize yourself with the DASH source code (especially interpreter.js and dash.pegjs). This is pretty good for learning how DASH works and will give you a wider range of options for your builtins. However, the code is not too readable as of now, so good luck 😬

Pay special attention to:

  • len: length calculation
  • tru, str, num, ls, obj, vr, app, pt, def, fn, a, rgx: convenience functions for wrapping/coercing into datatypes
  • form, sform: output formatting
  • pkg: gets packages by name
  • cm, vs: cm holds built-in functions, vs holds preinitialized variables
  • error, ERR: error display/formatting
  • ua: handling de Bruijn indices substitution for applications with def
  • I, exec: I simplifies the parse tree, exec decides when to stop executing I

##Small Example Assumes that all of these files (except for usage.dash) are contained in a package called x. ###pkg

x.dash

###x.dash

js rf "x.js"

It's cleaner to keep the Javascript in a separate file to avoid any backslash-escaping issues. ###x.js

cm.test=x=>str("Test passed!")

###usage.dash

pkg"x";test0

This should return "Test passed!"

Clone this wiki locally