SPAD Compiler

Pratt Compiler

For information about the Pratt compiler concept see this website:

http://eli.thegreenplace.net/2010/01/02/top-down-operator-precedence-parsing/

Boot Code

Boot code (Information about Boot Code in Axiom/FriCAS) was the forerunner to SPAD and in FriCAS it is still the language that the compiler is written in.

Here are some of the top level functions that are called when the compiler is called from the interpreter:

compiler boot code

Lex and Compiler

Text input:

)abbrev package F2A FriCAS2Aldor
++ Author: Martin Baker
++ Date Created: April 2017
++ Also See:
++ AMS Classifications:
++ Keywords:
++ Description:
++ This package builds XML structures which hold information about
++ SPAD code.

FriCAS2Aldor() : with

buildXmlAst : (filename : String) -> Void

== add
import XmlAttribute
import XmlElement
import ExportXml
import SExpression

toXml1(s : SExpression,lstName:String):XmlElement ==
--print ("parse toXml")$OutputForm
if float?(s) then
f:DoubleFloat := float s
a:XmlAttribute := xmlAttribute("float",mathObject2String(f)$Lisp :: String)
return xmlElement("float",[]$List(XmlElement),[a])
if integer?(s) then
i:Integer := integer s
a:XmlAttribute := xmlAttribute("int",string(i))
return xmlElement("int",[]$List(XmlElement),[a])
if string?(s) then
st:String := string s
a:XmlAttribute := xmlAttribute("string",st)
return xmlElement("string",[]$List(XmlElement),[a])
if symbol?(s) then
sy:Symbol := symbol s
a:XmlAttribute := xmlAttribute("symbol",string(sy))
return xmlElement("symbol",[]$List(XmlElement),[a])
if list?(s) then
lst: List(XmlElement) := [toXml1(x,"list") for x in destruct s]
return xmlElement(lstName,lst,[]$List(XmlAttribute))
xmlElement("error",[]$List(XmlElement),[]$List(XmlAttribute))

-- writeXml(content, filename) writes an xml element and all the
-- elements below it to an xml file. This assumes that all
-- elements, in a file, have a single root which is usually the case.
buildXmlAst(filename : String) : Void ==
f:SExpression := convert(filename)$SExpression
--res:SExpression := spadCompile(f)$Lisp -- int-top.boot
res:SExpression := parsespadCompile(f)$Lisp -- int-top.boot
funcEles : XmlElement := toXml1(res,"func")
ele:XmlElement := xmlElement("file",[funcEles],[]$List(XmlAttribute))
fileName : String := concat(["astxml/test",string(1),".xml"])$String
writeXml(ele,fileName)
void

TOK = <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<file>
<func>
[ [ id FriCAS2Aldor int="0" [ ]
[ posn [ int="0" " FriCAS2Aldor() : with" int="11"
int="11" string="fricas2aldor.spad" ] ] ]
[
symbol="key"
symbol="("
int="12"
symbol="nonblank"
[ symbol="posn"
[ int="0"
string="FriCAS2Aldor() : with"
int="11"
int="11"
string="fricas2aldor.spad" ] ] ]
[
symbol="key"
symbol=")"
int="13"
[ ]
[ symbol="posn"
[
int="0"
string="FriCAS2Aldor() : with"
int="11"
int="11"
string="fricas2aldor.spad"
]
]
Structured TOK = <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<file>
<func>
[
symbol="=="
[
symbol=":"
[
symbol="FriCAS2Aldor"
[
symbol="@Tuple"
]
]
[
symbol="with"
[
symbol="Signature"
symbol="buildXmlAst"
[
symbol="->"
[
symbol=":"
symbol="filename"
symbol="String"
]
symbol="Void"
]
[ ]
]
]
]
[
symbol="add"
[
symbol=";"
[
symbol=";"
[
symbol=";"
[
symbol=";"
[
symbol=";"
[
symbol="import"
symbol="XmlAttribute"
]
[
symbol="import"
symbol="XmlElement"
]
]
[
symbol="import"
symbol="ExportXml"
]
]
[
symbol="import"
symbol="SExpression"
]
]
[
symbol="=="
[
symbol=":"
[
symbol="toXml1"
[
symbol=","
[
symbol=":"
symbol="s"
symbol="SExpression"
]
[
symbol=":"
symbol="lstName"
symbol="String"
]
]
]
symbol="XmlElement"
]
[
symbol=";"
[
symbol=";"
[
symbol=";"
[
symbol=";"
[
symbol=";"
[
symbol="if"
[
symbol="float?"
symbol="s"
]
[
symbol=";"
[
symbol=";"
[
symbol=":="
[
symbol=":"
symbol="f"
symbol="DoubleFloat"
]
[
symbol="float"
symbol="s"
]
]
[
symbol=":="
[
symbol=":"
symbol="a"
symbol="XmlAttribute"
]
[
symbol="xmlAttribute"
[
symbol=","
<string string="float"
[
symbol="::"
[
[
symbol="Sel"
symbol="Lisp"
symbol="mathObject2String"
]
symbol="f"
]
symbol="String"
]
]
]
]
]
[
symbol="return"
[
symbol="xmlElement"
[
symbol=","
[
symbol=","
<string string="float"
[
[
symbol="Sel"
[
symbol="List"
symbol="XmlElement"
]
symbol="construct"
]
]
]
[
symbol="construct"
symbol="a"
]
]
]
]
]
[ ]
]
[
symbol="if"
[
symbol="integer?"
symbol="s"
]
[
symbol=";"
[
symbol=";"
[
symbol=":="
[
symbol=":"
symbol="i"
symbol="Integer"
]
[
symbol="integer"
symbol="s"
]
]
[
symbol=":="
[
symbol=":"
symbol="a"
symbol="XmlAttribute"
]
[
symbol="xmlAttribute"
[
symbol=","
<string string="int"
[
symbol="string"
symbol="i"
]
]
]
]
]
[
symbol="return"
[
symbol="xmlElement"
[
symbol=","
[
symbol=","
<string string="int"
[
[
symbol="Sel"
[
symbol="List"
symbol="XmlElement"
]
symbol="construct"
]
]
]
[
symbol="construct"
symbol="a"
]
]
]
]
]
[ ]

In S_process from ncomp.boot

 

nform :=
$noEarlyMacroexpand => x
walkForm x
null(nform) => nil
$ast := cons(nform, $ast)

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<file>
<func>
[
symbol="=="
[
symbol=":"
[
symbol="FriCAS2Aldor"
[
symbol="@Tuple"
]
]
[
symbol="with"
[
symbol="Signature"
symbol="buildXmlAst"
[
symbol="->"
[
symbol=":"
symbol="filename"
symbol="String"
]
symbol="Void"
]
[ ]
]
]
]
[
symbol="add"
[
symbol=";"
[
symbol=";"
[
symbol=";"
[
symbol=";"
[
symbol=";"
[
symbol="import"
symbol="XmlAttribute"
]
[
symbol="import"
symbol="XmlElement"
]
]
[
symbol="import"
symbol="ExportXml"
]
]
[
symbol="import"
symbol="SExpression"
]
]
[
symbol="=="
[
symbol=":"
[
symbol="toXml1"
[
symbol=","
[
symbol=":"
symbol="s"
symbol="SExpression"
]
[
symbol=":"
symbol="lstName"
symbol="String"
]
]
]
symbol="XmlElement"
]
[
symbol=";"
[
symbol=";"
[
symbol=";"
[
symbol=";"
[
symbol=";"
[
symbol="if"
[
symbol="float?"
symbol="s"
]
[
symbol=";"
[
symbol=";"
[
symbol=":="
[
symbol=":"
symbol="f"
symbol="DoubleFloat"
]
[
symbol="float"
symbol="s"
]
]
[
symbol=":="
[
symbol=":"
symbol="a"
symbol="XmlAttribute"
]
[
symbol="xmlAttribute"
[
symbol=","
<string string="float"
[
symbol="::"
[
[
symbol="Sel"
symbol="Lisp"
symbol="mathObject2String"
]
symbol="f"
]
symbol="String"
]
]
]
]
]
[
symbol="return"
[
symbol="xmlElement"
[
symbol=","
[
symbol=","
<string string="float"
[
[
symbol="Sel"
[
symbol="List"
symbol="XmlElement"
]
symbol="construct"
]
]
]
[
symbol="construct"
symbol="a"
]
]
]
]
]
[ ]
]
[
symbol="if"
[
symbol="integer?"
symbol="s"
]
[
symbol=";"
[
symbol=";"
[
symbol=":="
[
symbol=":"
symbol="i"
symbol="Integer"
]
  <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<file>
<func>
[ symbol="DEF"
[ symbol="FriCAS2Aldor" ]
[
[ symbol="CATEGORY" symbol="package"
[ symbol="SIGNATURE" symbol="buildXmlAst"
[
[ symbol="Void" ]
[ symbol="String" ]
]
]
]
]
[
[ ]
]
[
symbol="CAPSULE"
[
symbol="import"
[
symbol="XmlAttribute"
]
]
[
symbol="import"
[
symbol="XmlElement"
]
]
[
symbol="import"
[
symbol="ExportXml"
]
]
[
symbol="import"
[
symbol="SExpression"
]
]
[
symbol="DEF"
[
symbol="toXml1"
symbol="s"
symbol="lstName"
]
[
[
symbol="XmlElement"
]
[
symbol="SExpression"
]
[
symbol="String"
]
]
[
[ ]
[ ]
[ ]
]
[
symbol="SEQ"
[
symbol="SEQ"
[
symbol="LET"
[
symbol=":"
symbol="G705"
[
symbol="Boolean"
]
]
[
symbol="float?"
symbol="s"
]
]
[
symbol="exit"
<int int="1"
[
symbol="IF"
symbol="G705"
[
symbol="SEQ"
[
symbol="LET"
[
symbol=":"
symbol="f"
[
symbol="DoubleFloat"
]
]
[
symbol="float"
symbol="s"
]
]
[
symbol="LET"
[
symbol=":"
symbol="a"
[
symbol="XmlAttribute"
]
]
[
symbol="xmlAttribute"
<string string="float"
[
symbol="::"
[
[
symbol="Sel"
symbol="Lisp"
symbol="mathObject2String"
]
symbol="f"
]
[
symbol="String"
]
]
]
]
[
symbol="exit"
<int int="1"
[
symbol="return"
[
symbol="xmlElement"
<string string="float"
[
[
symbol="Sel"
[
symbol="List"
[
symbol="XmlElement"
]
]
symbol="construct"
]
]
[
symbol="construct"
symbol="a"
]
]
]
]
]
symbol="noBranch"
]
]
]
[
symbol="SEQ"
[
symbol="LET"
[
symbol=":"
symbol="G706"
[
symbol="Boolean"
]
]
[
symbol="integer?"
symbol="s"
]
]
[
symbol="exit"
<int int="1"
[
symbol="IF"
symbol="G706"
[
symbol="SEQ"
[
symbol="LET"
[
symbol=":"
symbol="i"
[
symbol="Integer"
]
]
[
symbol="integer"
symbol="s"
]
]
[
symbol="LET"
[
symbol=":"
symbol="a"
[
symbol="XmlAttribute"
]
]
[
symbol="xmlAttribute"
<string string="int"
[
symbol="string"
symbol="i"
]
]
]
[
symbol="exit"
<int int="1"
[
symbol="return"
[
symbol="xmlElement"
<string string="int"
[
[
symbol="Sel"
[
symbol="List"
[
symbol="XmlElement"
]
]
symbol="construct"
]
]
[
symbol="construct"
symbol="a"
]
]
]
]
]
symbol="noBranch"
]
]
]
[
symbol="SEQ"
[
symbol="LET"
[
symbol=":"
symbol="G707"
[
symbol="Boolean"
]
]
[
symbol="string?"
symbol="s"
]
]
[
symbol="exit"
<int int="1"
[
symbol="IF"
symbol="G707"
[
symbol="SEQ"
[
symbol="LET"
[
symbol=":"
symbol="st"
[
symbol="String"
]
]
[
symbol="string"
symbol="s"
]
]
[
symbol="LET"
[
symbol=":"
symbol="a"
[
symbol="XmlAttribute"
]
]
[
symbol="xmlAttribute"
<string string="string"
symbol="st"
]
]
[
symbol="exit"
<int int="1"
[
symbol="return"
[
symbol="xmlElement"
<string string="string"
[
[
symbol="Sel"
[
symbol="List"
[
symbol="XmlElement"
]
]
symbol="construct"
]
]
[
symbol="construct"
symbol="a"
]
]
]
]
]
symbol="noBranch"
]
]
]
[
symbol="SEQ"
[
symbol="LET"
[
symbol=":"
symbol="G708"
[
symbol="Boolean"
]
]
[
symbol="symbol?"
symbol="s"
]
]
[
symbol="exit"
<int int="1"
[
symbol="IF"
symbol="G708"
[
symbol="SEQ"
[
symbol="LET"
[
symbol=":"
symbol="sy"
[
symbol="Symbol"
]
]
[
symbol="symbol"
symbol="s"
]
]
[
symbol="LET"
[
symbol=":"
symbol="a"
[
symbol="XmlAttribute"
]
]
[
symbol="xmlAttribute"
<string string="symbol"
[
symbol="string"
symbol="sy"
]
]
]
[
symbol="exit"
<int int="1"
[
symbol="return"
[
symbol="xmlElement"
<string string="symbol"
[
[
symbol="Sel"
[
symbol="List"
[
symbol="XmlElement"
]
]
symbol="construct"
]
]
[
symbol="construct"
symbol="a"
]
]
]
]
]
symbol="noBranch"
]
]
]
[
symbol="SEQ"
[
symbol="LET"
[
symbol=":"
symbol="G709"
[
symbol="Boolean"
]
]
[
symbol="list?"
symbol="s"
]
]
[
symbol="exit"
<int int="1"
[
symbol="IF"
symbol="G709"
[
symbol="SEQ"
[
symbol="LET"
[
symbol=":"
symbol="lst"
[
symbol="List"
[
symbol="XmlElement"
]
]
]
[
symbol="COLLECT"
[
symbol="IN"
symbol="x"
[
symbol="destruct"
symbol="s"
]
]
[
symbol="toXml1"
symbol="x"
<string string="list"
]
]
]
[
symbol="exit"
<int int="1"
[
symbol="return"
[
symbol="xmlElement"
symbol="lstName"
symbol="lst"
[
[
symbol="Sel"
[
symbol="List"
[
symbol="XmlAttribute"
]
]
symbol="construct"
]
]
]
]
]
]
symbol="noBranch"
]
]
]
[
symbol="exit"
<int int="1"
[
symbol="xmlElement"
<string string="error"
[
[
symbol="Sel"
[
symbol="List"
[
symbol="XmlElement"
]
]
symbol="construct"
]
]
[
[
symbol="Sel"
[
symbol="List"
[
symbol="XmlAttribute"
]
]
symbol="construct"
]
]
]
]
]
]
[
symbol="DEF"
[
symbol="buildXmlAst"
symbol="filename"
]
[
[
symbol="Void"
]
[
symbol="String"
]
]
[
[ ]
[ ]
]
[
symbol="SEQ"
[
symbol="LET"
[
symbol=":"
symbol="f"
[
symbol="SExpression"
]
]
[
[
symbol="Sel"
[
symbol="SExpression"
]
symbol="convert"
]
symbol="filename"
]
]
[
symbol="LET"
[
symbol=":"
symbol="res"
[
symbol="SExpression"
]
]
[
[
symbol="Sel"
symbol="Lisp"
symbol="parsespadCompile"
]
symbol="f"
]
]
[
symbol="LET"
[
symbol=":"
symbol="funcEles"
[
symbol="XmlElement"
]
]
[
symbol="toXml1"
symbol="res"
<string string="func"
]
]
[
symbol="LET"
[
symbol=":"
symbol="ele"
[
symbol="XmlElement"
]
]
[
symbol="xmlElement"
<string string="file"
[
symbol="construct"
symbol="funcEles"
]
[
[
symbol="Sel"
[
symbol="List"
[
symbol="XmlAttribute"
]
]
symbol="construct"
]
]
]
]
[
symbol="LET"
[
symbol=":"
symbol="fileName"
[
symbol="String"
]
]
[
[
symbol="Sel"
[
symbol="String"
]
symbol="concat"
]
[
symbol="construct"
<string string="astxml/test"
[
symbol="string"
[
symbol="One"
]
]
<string string=".xml"
]
]
]
[
symbol="writeXml"
symbol="ele"
symbol="fileName"
]
[
symbol="exit"
<int int="1"
symbol="void"
]
]
]
]
]
</func>
</file>
u <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<file>
<func>
<list>
<symbol symbol="FriCAS2Aldor" />
<list>
<symbol symbol="Mapping" />
<list>
<symbol symbol="CATEGORY" />
<symbol symbol="package" />
<list>
<symbol symbol="SIGNATURE" />
<symbol symbol="buildXmlAst" />
<list>
<list>
<symbol symbol="Void" />
</list>
<list>
<symbol symbol="String" />
</list>
</list>
</list>
</list>
</list>
<list>
<list>
<list>
<list>
<symbol symbol="$DomainsInScope" />
<list>
<symbol symbol="FLUID" />
</list>
<list>
<symbol symbol="special" />
<symbol symbol="$EmptyMode" />
<symbol symbol="$NoValueMode" />
</list>
</list>
</list>
</list>
</list>
</list>
</func>
</file>

 

Code Chasing

It is very difficult to work out axiom/FriCAS code. Especially something like the compiler, there is very little documentation or comments. It is specially hard to work out the overall structure of the program and why certain things were done in the way they were.

This page is just a few random notes of mine, all I have done is trace through a few paths through the code.

This first bit just seems to parse the parameters supplied in the )compile command line.

compiler

 

(2) -> pn:SExpression := pathname("clifford.spad")$Lisp

   (2)  clifford.spad
                                                            Type: SExpression
(3) -> npParse(pn)$Lisp 
      
   >> System error:  
   The value #P"clifford.spad" is not of type LIST. 
                                                                        
(3) ->  

fricas / src / interp / scwrap2.boot

(8) -> spadCompile("clifford.spad")$Lisp
   CLIF abbreviates domain CliffordAlgebra 
 
   >> System error:
   The function BOOT::|getSignatureDocumentation| is undefined.

(8) -> fakeloopInclude("clifford.spad",nil)$Lisp
   CLIF abbreviates domain CliffordAlgebra 
   Internal Error
   Unexpected error or improper call to system function coerce : 
      function coerce called from the interpreter.

(8) -> 

 

fricas / src / interp / i-syscmd.boot

'parseFromString' is called to take user input and convert it into an SExpression.
(1) -> parseFromString("1")$Lisp

   (1)  1
                                                            Type: SExpression
(2) -> parseFromString("a")$Lisp

   (2)  a
                                                            Type: SExpression
(3) -> parseFromString("1.2")$Lisp

   (3)  (($elt (Float) float) 12 - 1 10)
                                                            Type: SExpression
(4) -> parseFromString("%a")$Lisp

   (4)  %a
                                                            Type: SExpression
(5) -> parseFromString("2*3.4")$Lisp

   (5)  (* 2 (($elt (Float) float) 34 - 1 10))
                                                            Type: SExpression 

Parse.boot

compiler code gen

(1) -> parseTran(a)$Lisp

   (1)  a
                                                            Type: SExpression
(2) -> parseTran(a+b)$Lisp

   (2)  (1 b (1 0) (0 1 a (1 0)))
                                                            Type: SExpression
(3) -> parseTran(P ==> PositiveInteger)$Lisp

   (3)  ()
                                                            Type: SExpression
(4) -> s:SExpression := P ==> PositiveInteger
 
   You cannot assign an object of type Void to any identifier, (in 
      particular, ??? ).
(4) -> 

SPAD Translation

 


metadata block
see also:
Correspondence about this page

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2023 Martin John Baker - All rights reserved - privacy policy.