Constructor
new Converter()
Creates a new converter. No arguments are required. To customize the converter to your needs, refer to the documentation at the top of the class, which lists a workflow for how you should call the other functions in this class to set an instance up for your needs.
Source
Classes
Methods
addBuiltIns(namesopt)
A set of common mathematical concepts are built into this repository. (See the file built-in-concepts.js.) This function can import all or some of those concepts into this converter.
If you call it with no arguments, all the built-in concepts are imported, including basic operators and concepts from arithmetic, algebra, logic, naive set theory, functions, relations, and more.
If you call it with an array of strings, they are treated as the names of the concepts you want imported. If those concepts are defined in terms of other concepts, then the others are also imported, and so on, recursively. (For example, even if you do not import the concepts of variables, if you were to import the concept of quantifiers, they require variables, and thus those concepts would be imported also.)
Parameters
-
names
Array.<String>
<optional>
the names of those concepts to import, or leave empty to import all
Source
addConcept(name, parentType, putdown, optionsnullable)
The SyntacticTypes module defines a set of types called "syntactic types" (explained in greater detail in that module) that make it easier to define a language. Converter instances define their own set of "semantic types" that sit within the hierarchy established by those syntactic types. Such "semantic types" are called "concepts" and you add them to a converter using this function.
The most complex parameter here is the third one, which specifies the putdown notation for the concept. This can be one of two things:
- If it is a regular expression, it will be used during tokenization, to
find portions of input in this language that represent this concept.
For example, if you want your language to include integers, you might
call this function, passing the word "integer" as the concept name,
"atomicnumber" as the parent type, and a regular expression like
/-?[0-9]+/
as the putdown notation. This not only makes that regular expression the putdown notation, but it also makes it the default notation for all future languages added to this converter. Clients can override that default with later calls to addNotation(). - If it is a string, it should be appropriate putdown code for an
application or binding, and its leaves may include any syntactic or
semantic type name, to restrict parsing appropriately. For example,
if you want your language to include addition of integers, you might
call this function, passing the word "intsum" as the concept name,
"sum" as the parent type, and the putdown notation
"(+ integer integer)"
.
Note that this function does not allow you to specify how the concept is written in any language other than putdown. To do so, you must make calls to addNotation().
The final parameter is an options object, which supports the following fields.
primitive
- this allows you to specify whether this concept is a primitive concept (the default) or a derived concept (by setting the option tofalse
). See the documentation for this class for an overview of primitive vs. derived concepts. The final parameter is ignored in the case whereputdown
is a regular expression, because such a value makes sense only if the concept is primitive.associative
- this allows you to specify whether this concept functions as an associative operator, in the sense that nested copies of it should be flattened out into a single copy with more arguments. The default is that the concept is not associative, which will cause most operators to associate to the right, so that, for example,A -> B -> C
is stored internally asA -> (B -> C)
. You can change this behavior by setting the option to a list of other concepts that, if they show up as children of this one, will be flattened into this one. For example, for a concept "add" you might use["add"]
to say that it only flattens into itself. But if you have multiple ways to add numbers, you can add all the concepts to the list. You typically want to do this in the definitions for all of those concepts, forming an equivalence class, so all will merge with one another in any order in which they might be nested.associates
- this allows you to specify, for concepts that are binary operators, how they should be parsed when they are nested without groupers to disambiguate. For example, in the case of ordinary addition, if we write1+2+3
, does it mean(+ (+ 1 2) 3)
or(+ 1 (+ 2 3))
? The default is to permit both forms, and whichever is alphabetically sooner will be the default returned by parsing. (Also, varioius parsing functions like parse() and convertTo() allow you to specify whether to return all parsings in the case of ambiguity, or stick with the default of returning just one.) If you set theassociates
option to"left"
, then all parsings that contain structures of the form(+ x (+ y z))
will be omitted from the possible results. If you set it to"right"
then all parsings with(+ (+ x y) z)
are omitted. If you do not set this option, then all parsings are permitted.
Parameters
-
name
String
the name of the concept to add
-
parentType
String
the name of the parent type, which must be a syntactic type
-
putdown
String
|RegExp
the notation for this concept in the putdown language
-
options
Object
<nullable>
see supported fields above
Source
concept(name) → {Object}
Returns the concept with the given name, if any. Concepts are added by calls to addConcept().
Parameters
-
name
String
the name of the concept to retrieve
Returns
-
Object
the concept with the given name
Source
convert(sourceLang, destLang, text, ambiguousopt) → {String|Array.<String>}
Use this converter to convert text in any language it knows into text in
any of the other languages it knows. For example,
converter.convert( 'putdown', 'latex', input )
parses the given input
as putdown notation and returns LaTeX (assuming that you have defined a
LaTeX language in this converter; you can use any language you have
defined in place of LaTeX). Similarly, you can convert into putdown from
LaTeX, or between two non-putdown languages.
Parameters
-
sourceLang
String
the name of the language in which the input is expressed
-
destLang
String
the name of the language into which to convert the input
-
text
String
the input to be converted
-
ambiguous
boolean
<optional>
falsepassed to the parse() function, and thus determines whether the result of this is a string or an array thereof (default is false)
See
Returns
-
String
Array.<String>
the converted output(s)
Source
isConcept(name) → {boolean}
Does this converter know a concept with the given name?
Parameters
-
name
String
the name of the concept to check
Returns
-
boolean
whether a concept with that name has been added to this converter
Source
isLanguage(name) → {boolean}
Does this converter have a language of the given name?
Parameters
-
name
String
the name of the language to check
Returns
-
boolean
whether a language with that name has been added to this converter
Source
language(name) → {Language}
Returns the language with the given name, if any. Languages are added by calls to the Language constructor.
Parameters
-
name
String
the name of the language to retrieve
See
Returns
-
Language
the language with the given name