value.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. package attl
  2. type Attl struct {
  3. index int
  4. input string
  5. }
  6. type Proc func(*Environment, ...Value) (Value, Effect)
  7. func (pv Proc) String() string {
  8. return "proc"
  9. }
  10. // Evaler is an interface to a Value that can evaluate itself
  11. // based on and possibly modifying a given environment.
  12. type Evaler interface {
  13. Eval(*Environment, ...Value) (Value, Effect)
  14. }
  15. // Lazyer is an interface to a Value that does not automatically
  16. // evaluate itself in List and similar contexts.
  17. type Lazyer interface {
  18. // Lazy is a marker method only.
  19. Lazy()
  20. }
  21. // Value is an interface for the basic unit of data that
  22. // the ATTL interpreter works with.
  23. // It is used both for run time data and parsing data.
  24. type Value interface {
  25. // Since ATTL is TCL-like, all values must be
  26. // convertable to string
  27. String() string
  28. // Furthermore any ATTL value must be able to evaluate itself.
  29. Evaler
  30. }
  31. // Typer is an interface that Values can optionally implement to
  32. // allow them to report their type.
  33. type Typer interface {
  34. Type() Type
  35. }
  36. type Int int
  37. type Bool bool
  38. type String string
  39. type Word string
  40. type Type string
  41. type Comment string
  42. type Error struct {
  43. Message string
  44. Index int
  45. Children List
  46. }
  47. type List []Value
  48. type Map map[string]Value
  49. func (m Map) Keys() List {
  50. res := List{}
  51. for k, _ := range m {
  52. res = append(res, String(k))
  53. }
  54. return res
  55. }
  56. func (m Map) SortedKeys() List {
  57. return m.Keys().SortStrings()
  58. }
  59. type Getter struct {
  60. Key Value
  61. }
  62. type Evaluation struct {
  63. Command
  64. }
  65. type Command struct {
  66. Order Value
  67. Parameters List
  68. }
  69. type Block struct {
  70. Statements List
  71. }
  72. type Defined struct {
  73. Name string
  74. Params List
  75. Block
  76. }
  77. type Wrapper struct {
  78. Kind Type
  79. Handle interface{}
  80. Methods Map
  81. }
  82. type Object struct {
  83. Wrapper
  84. Fields Map
  85. Embedded Map
  86. }
  87. type Overload Map
  88. func (bv Bool) String() string {
  89. if bv {
  90. return "true"
  91. }
  92. return "false"
  93. }
  94. func (sv String) String() string {
  95. return string(sv)
  96. }
  97. func (cv Comment) String() string {
  98. return string(cv)
  99. }
  100. func (sv Word) String() string {
  101. return string(sv)
  102. }
  103. func (tv Type) String() string {
  104. return string(tv)
  105. }
  106. func (gv Getter) String() string {
  107. return gv.Key.String()
  108. }
  109. func (cv Command) String() string {
  110. return cv.Order.String() + " " + cv.Parameters.String()
  111. }
  112. func (gv Evaluation) String() string {
  113. return gv.Command.String()
  114. }
  115. func (bv Block) String() string {
  116. return "{" + bv.Statements.String() + "}"
  117. }
  118. func (dv Defined) String() string {
  119. return "to " + dv.Name + " (" +
  120. dv.Params.String() + ") " + dv.Block.String()
  121. }
  122. func (cv Overload) String() string {
  123. return Map(cv).String()
  124. }
  125. // Lazy marks that Blocks should be lazily evaluated.
  126. func (Block) Lazy() {}
  127. func (iv Int) String() string {
  128. return Itoa(int(iv))
  129. }
  130. func (lv Map) String() string {
  131. aid := "[map "
  132. for k, v := range lv {
  133. aid += " "
  134. aid += k
  135. aid += " "
  136. aid += v.String()
  137. }
  138. aid += "]"
  139. return aid
  140. }
  141. func (lv List) String() string {
  142. aid := "[list"
  143. for _, v := range lv {
  144. aid += " "
  145. if v == nil {
  146. aid += "nil"
  147. } else {
  148. aid += v.String()
  149. }
  150. }
  151. aid += "]"
  152. return aid
  153. }
  154. func (ev Error) String() string {
  155. return string(ev.Message)
  156. }
  157. func (ev Error) Error() string {
  158. return ev.Message
  159. }
  160. // Implement the effect interface
  161. func (ev *Error) Flow() Flow {
  162. if ev == nil {
  163. return NormalFlow
  164. }
  165. return FailFlow
  166. }
  167. func (ev *Error) Unwrap() Value {
  168. if ev == nil {
  169. return nil
  170. }
  171. return ev
  172. }
  173. func NewError(message string, index int, children ...Value) *Error {
  174. return &Error{message, index, children}
  175. }
  176. func ErrorFromString(message string) *Error {
  177. return NewError(message, -1)
  178. }
  179. func ErrorFromError(err error, children ...Value) *Error {
  180. if err == nil {
  181. return nil
  182. }
  183. return NewError(err.Error(), -1, children...)
  184. }
  185. // Break is used for break flows
  186. type Break struct {
  187. Value // value returned by break
  188. }
  189. func (bv Break) Flow() Flow {
  190. return BreakFlow
  191. }
  192. func (bv Break) Unwrap() Value {
  193. return bv.Value
  194. }
  195. // Return is used for return flows
  196. type Return struct {
  197. Value // value returned
  198. }
  199. func (rv Return) Flow() Flow {
  200. return ReturnFlow
  201. }
  202. func (bv Return) Unwrap() Value {
  203. return bv.Value
  204. }
  205. // Rescue is used to evaluate rescue commands
  206. type Rescue struct {
  207. Block // A rescue is a special block
  208. }
  209. // Rescue essentially protects the block from
  210. // rescue recursion by pushing the stack once.
  211. func (r Rescue) Eval(env *Environment, args ...Value) (Value, Effect) {
  212. // ignore the stack depth
  213. // protection here to be sure the
  214. // rescue block is executed
  215. _ = env.Push()
  216. env.Rescuing = true
  217. env.Printi("Rescuing.\n")
  218. defer env.Pop()
  219. defer func() {
  220. env.Rescuing = false
  221. }()
  222. return r.Block.Eval(env, args...)
  223. }
  224. func (iv Wrapper) String() string {
  225. aid := "[interface"
  226. aid += " " + iv.Kind.String()
  227. aid += " " + iv.Methods.String()
  228. aid += "]"
  229. return aid
  230. }
  231. func (sv Object) String() string {
  232. aid := "[struct"
  233. aid += " " + sv.Kind.String()
  234. aid += " " + sv.Methods.String()
  235. aid += " " + sv.Fields.String()
  236. aid += " " + sv.Embedded.String()
  237. aid += "]"
  238. return aid
  239. }
  240. func NewAttl(input string) Attl {
  241. return Attl{0, input}
  242. }
  243. func (wv Word) Eval(env *Environment, args ...Value) (Value, Effect) {
  244. return wv, nil
  245. }
  246. func (sv String) Eval(env *Environment, args ...Value) (Value, Effect) {
  247. return sv, nil
  248. }
  249. func (tv Type) Eval(env *Environment, args ...Value) (Value, Effect) {
  250. return tv, nil
  251. }
  252. func (iv Int) Eval(env *Environment, args ...Value) (Value, Effect) {
  253. return iv, nil
  254. }
  255. func (bv Bool) Eval(env *Environment, args ...Value) (Value, Effect) {
  256. return bv, nil
  257. }
  258. func (ev Error) Eval(env *Environment, args ...Value) (Value, Effect) {
  259. return ev, nil
  260. }
  261. // Eval of a List expands arguments, except Lazyer elements.
  262. func (lv List) Eval(env *Environment, args ...Value) (Value, Effect) {
  263. res := List{}
  264. for _, s := range lv {
  265. _, isLazy := s.(Lazyer)
  266. if isLazy {
  267. res = append(res, s)
  268. } else {
  269. val, err := s.Eval(env, args...)
  270. if err != nil {
  271. return val, err
  272. }
  273. res = append(res, val)
  274. }
  275. }
  276. return res, nil
  277. }
  278. func (mv Map) Eval(env *Environment, args ...Value) (Value, Effect) {
  279. return mv, nil
  280. }
  281. func (cv Comment) Eval(env *Environment, args ...Value) (Value, Effect) {
  282. return nil, nil
  283. }
  284. func (bv Block) Eval(env *Environment, args ...Value) (Value, Effect) {
  285. var res Value
  286. var eff Effect
  287. // set parameters to $1 ... $(len(args))
  288. for i, a := range args {
  289. name := Itoa(i + 1)
  290. env.Define(name, a, 0)
  291. }
  292. // Set $argc to amount of arguments
  293. // and $argv to arguments as well
  294. env.Define("argc", Int(len(args)), 0)
  295. env.Define("argv", List(args), 0)
  296. for _, s := range bv.Statements {
  297. // Call the statement.
  298. res, eff = s.Eval(env, args...)
  299. // if the flow is not normal anymore,
  300. // end the block execution at this point
  301. if eff != nil && eff.Flow() > NormalFlow {
  302. // If it was a break, unwrap and done,
  303. if eff.Flow() <= BreakFlow {
  304. return eff.Unwrap(), nil
  305. } else if eff.Flow() == FailFlow {
  306. // If it is a fail try to rescue it
  307. return env.Rescue(res, eff)
  308. }
  309. return res, eff
  310. }
  311. env.Define("RESULT", res, 0)
  312. }
  313. return res, eff
  314. }
  315. func (pv Proc) Eval(env *Environment, args ...Value) (Value, Effect) {
  316. return pv(env, args...)
  317. }
  318. func (cv Command) Eval(env *Environment, args ...Value) (Value, Effect) {
  319. val, eff := cv.Order.Eval(env)
  320. if eff != nil || val == nil {
  321. return val, eff
  322. }
  323. name := val.String()
  324. fun := env.Lookup(name)
  325. if fun == nil {
  326. return nil, ErrorFromString("Cannot evaluate nil order: " + name)
  327. }
  328. eva, ok := fun.(Evaler)
  329. if !ok {
  330. return nil, ErrorFromString("Cannot evaluate: " + name)
  331. }
  332. err := env.Push()
  333. // stack depth protection
  334. if err != nil {
  335. return env.Rescue(env.Fail(err))
  336. }
  337. defer env.Pop()
  338. fargs := cv.Parameters
  339. // Expand Evaluation arguments, but not block elements.
  340. eargs, eff := fargs.Eval(env, args...)
  341. if eff != nil {
  342. return nil, eff
  343. }
  344. return eva.Eval(env, eargs.(List)...)
  345. }
  346. func (gv Getter) Eval(env *Environment, args ...Value) (Value, Effect) {
  347. val, err := gv.Key.Eval(env)
  348. if err != nil || val == nil {
  349. return val, err
  350. }
  351. return env.Lookup(val.String()), nil
  352. }
  353. func (ev Evaluation) Eval(env *Environment, args ...Value) (Value, Effect) {
  354. err := env.Push()
  355. // stack depth protection
  356. if err != nil {
  357. return env.Fail(err)
  358. }
  359. defer env.Pop()
  360. val, eff := ev.Command.Eval(env, args...)
  361. return val, eff
  362. }
  363. func (dv Defined) Eval(env *Environment, args ...Value) (Value, Effect) {
  364. err := env.Push()
  365. // stack depth protection
  366. if err != nil {
  367. return env.Rescue(env.Fail(err))
  368. }
  369. if len(dv.Params) > len(args) {
  370. return env.FailString("Not enough arguments")
  371. }
  372. for i := 0; i < len(dv.Params); i++ {
  373. env.Define(dv.Params[i].String(), args[i], 0)
  374. }
  375. // $0 contains the name of the defined procedure
  376. env.Define("0", String(dv.Name), 0)
  377. defer env.Pop()
  378. val, eff := dv.Block.Eval(env, args...)
  379. if eff == nil || eff.Flow() < ReturnFlow {
  380. return val, nil
  381. } else if eff.Flow() == ReturnFlow {
  382. return eff.Unwrap(), nil
  383. } else { // failures pass through
  384. return val, eff
  385. }
  386. }
  387. func (iv Wrapper) Eval(env *Environment, args ...Value) (Value, Effect) {
  388. // Object like values such as interfaces or structs
  389. // have methods that are called with the method
  390. // name as the first word argument, which is used
  391. // to dispatch the function.
  392. // The dispathed function receives the object
  393. // as it's first argument
  394. var name Word
  395. err := Args(args, &name)
  396. if err != nil {
  397. return env.Fail(err)
  398. }
  399. method, ok := iv.Methods[name.String()]
  400. if !ok {
  401. return env.FailString("No such method ${1}", name)
  402. }
  403. args[0] = iv
  404. return method.Eval(env, args...)
  405. }
  406. func (sv Object) Eval(env *Environment, args ...Value) (Value, Effect) {
  407. // See interface for this dispatch
  408. var name Word
  409. err := Args(args, &name)
  410. if err != nil {
  411. return env.Fail(err)
  412. }
  413. method, ok := sv.Methods[name.String()]
  414. if !ok {
  415. return env.FailString("No such method ${1}", name)
  416. }
  417. args[0] = sv
  418. return method.Eval(env, args...)
  419. }
  420. func TypeOf(val Value) Type {
  421. if typer, ok := val.(Typer); ok {
  422. return typer.Type()
  423. } else {
  424. return Type("Unknown")
  425. }
  426. }
  427. func (cv Overload) Eval(env *Environment, args ...Value) (Value, Effect) {
  428. signature := ""
  429. for _, arg := range args {
  430. signature += "_" + TypeOf(arg).String()
  431. }
  432. target, ok := cv[signature]
  433. if ok {
  434. return target.Eval(env, args...)
  435. }
  436. return env.FailString("No overload defined for signature: " + signature)
  437. }
  438. // Implement Typer interface for commonly used Values
  439. func (String) Type() Type { return Type("String") }
  440. func (Bool) Type() Type { return Type("Bool") }
  441. func (Int) Type() Type { return Type("Int") }
  442. func (Error) Type() Type { return Type("Error") }
  443. func (List) Type() Type { return Type("List") }
  444. func (Map) Type() Type { return Type("Map") }
  445. func (Proc) Type() Type { return Type("Proc") }
  446. func (Word) Type() Type { return Type("Word") }
  447. func (Defined) Type() Type { return Type("Defined") }
  448. func (Block) Type() Type { return Type("Block") }
  449. func (Command) Type() Type { return Type("Command") }
  450. func (Getter) Type() Type { return Type("Getter") }
  451. func (Evaluation) Type() Type { return Type("Evaluation") }
  452. func (t Type) Type() Type { return Type("Type") }
  453. func (s Object) Type() Type { return s.Kind }
  454. func (i Wrapper) Type() Type { return i.Kind }
  455. func (t Type) Overload() Type { return Type("Overload") }