objects.go 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  1. package tengo
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/d5/tengo/v2/parser"
  10. "github.com/d5/tengo/v2/token"
  11. )
  12. var (
  13. // TrueValue represents a true value.
  14. TrueValue Object = Bool{value: true}
  15. // FalseValue represents a false value.
  16. FalseValue Object = Bool{value: false}
  17. // UndefinedValue represents an undefined value.
  18. UndefinedValue Object = &Undefined{}
  19. )
  20. // Object represents an object in the VM.
  21. type Object interface {
  22. // TypeName should return the name of the type.
  23. TypeName() string
  24. // String should return a string representation of the type's value.
  25. String() string
  26. // BinaryOp should return another object that is the result of a given
  27. // binary operator and a right-hand side object. If BinaryOp returns an
  28. // error, the VM will treat it as a run-time error.
  29. BinaryOp(op token.Token, rhs Object) (Object, error)
  30. // IsFalsy should return true if the value of the type should be considered
  31. // as falsy.
  32. IsFalsy() bool
  33. // Equals should return true if the value of the type should be considered
  34. // as equal to the value of another object.
  35. Equals(another Object) bool
  36. // Copy should return a copy of the type (and its value). Copy function
  37. // will be used for copy() builtin function which is expected to deep-copy
  38. // the values generally.
  39. Copy() Object
  40. // IndexGet should take an index Object and return a result Object or an
  41. // error for indexable objects. Indexable is an object that can take an
  42. // index and return an object. If error is returned, the runtime will treat
  43. // it as a run-time error and ignore returned value. If Object is not
  44. // indexable, ErrNotIndexable should be returned as error. If nil is
  45. // returned as value, it will be converted to UndefinedToken value by the
  46. // runtime.
  47. IndexGet(index Object) (value Object, err error)
  48. // IndexSet should take an index Object and a value Object for index
  49. // assignable objects. Index assignable is an object that can take an index
  50. // and a value on the left-hand side of the assignment statement. If Object
  51. // is not index assignable, ErrNotIndexAssignable should be returned as
  52. // error. If an error is returned, it will be treated as a run-time error.
  53. IndexSet(index, value Object) error
  54. // Iterate should return an Iterator for the type.
  55. Iterate() Iterator
  56. // CanIterate should return whether the Object can be Iterated.
  57. CanIterate() bool
  58. // Call should take an arbitrary number of arguments and returns a return
  59. // value and/or an error, which the VM will consider as a run-time error.
  60. Call(args ...Object) (ret Object, err error)
  61. // CanCall should return whether the Object can be Called.
  62. CanCall() bool
  63. }
  64. // ObjectImpl represents a default Object Implementation. To defined a new
  65. // value type, one can embed ObjectImpl in their type declarations to avoid
  66. // implementing all non-significant methods. TypeName() and String() methods
  67. // still need to be implemented.
  68. type ObjectImpl struct {
  69. }
  70. // TypeName returns the name of the type.
  71. func (o ObjectImpl) TypeName() string {
  72. panic(ErrNotImplemented)
  73. }
  74. func (o ObjectImpl) String() string {
  75. panic(ErrNotImplemented)
  76. }
  77. // BinaryOp returns another object that is the result of a given binary
  78. // operator and a right-hand side object.
  79. func (o ObjectImpl) BinaryOp(_ token.Token, _ Object) (Object, error) {
  80. return nil, ErrInvalidOperator
  81. }
  82. // Copy returns a copy of the type.
  83. func (o ObjectImpl) Copy() Object {
  84. return nil
  85. }
  86. // IsFalsy returns true if the value of the type is falsy.
  87. func (o ObjectImpl) IsFalsy() bool {
  88. return false
  89. }
  90. // Equals returns true if the value of the type is equal to the value of
  91. // another object.
  92. func (o ObjectImpl) Equals(x Object) bool {
  93. return o == x
  94. }
  95. // IndexGet returns an element at a given index.
  96. func (o ObjectImpl) IndexGet(_ Object) (res Object, err error) {
  97. return nil, ErrNotIndexable
  98. }
  99. // IndexSet sets an element at a given index.
  100. func (o ObjectImpl) IndexSet(_, _ Object) (err error) {
  101. return ErrNotIndexAssignable
  102. }
  103. // Iterate returns an iterator.
  104. func (o ObjectImpl) Iterate() Iterator {
  105. return nil
  106. }
  107. // CanIterate returns whether the Object can be Iterated.
  108. func (o ObjectImpl) CanIterate() bool {
  109. return false
  110. }
  111. // Call takes an arbitrary number of arguments and returns a return value
  112. // and/or an error.
  113. func (o ObjectImpl) Call(_ ...Object) (ret Object, err error) {
  114. return nil, nil
  115. }
  116. // CanCall returns whether the Object can be Called.
  117. func (o ObjectImpl) CanCall() bool {
  118. return false
  119. }
  120. // PtrObjectImpl represents a default Object Implementation. To defined a new
  121. // value type, one can embed PtrObjectImpl in their type declarations to avoid
  122. // implementing all non-significant methods. TypeName() and String() methods
  123. // still need to be implemented.
  124. type PtrObjectImpl struct {
  125. }
  126. // TypeName returns the name of the type.
  127. func (o *PtrObjectImpl) TypeName() string {
  128. panic(ErrNotImplemented)
  129. }
  130. func (o *PtrObjectImpl) String() string {
  131. panic(ErrNotImplemented)
  132. }
  133. // BinaryOp returns another object that is the result of a given binary
  134. // operator and a right-hand side object.
  135. func (o *PtrObjectImpl) BinaryOp(_ token.Token, _ Object) (Object, error) {
  136. return nil, ErrInvalidOperator
  137. }
  138. // Copy returns a copy of the type.
  139. func (o *PtrObjectImpl) Copy() Object {
  140. return nil
  141. }
  142. // IsFalsy returns true if the value of the type is falsy.
  143. func (o *PtrObjectImpl) IsFalsy() bool {
  144. return false
  145. }
  146. // Equals returns true if the value of the type is equal to the value of
  147. // another object.
  148. func (o *PtrObjectImpl) Equals(x Object) bool {
  149. return o == x
  150. }
  151. // IndexGet returns an element at a given index.
  152. func (o *PtrObjectImpl) IndexGet(_ Object) (res Object, err error) {
  153. return nil, ErrNotIndexable
  154. }
  155. // IndexSet sets an element at a given index.
  156. func (o *PtrObjectImpl) IndexSet(_, _ Object) (err error) {
  157. return ErrNotIndexAssignable
  158. }
  159. // Iterate returns an iterator.
  160. func (o *PtrObjectImpl) Iterate() Iterator {
  161. return nil
  162. }
  163. // CanIterate returns whether the Object can be Iterated.
  164. func (o *PtrObjectImpl) CanIterate() bool {
  165. return false
  166. }
  167. // Call takes an arbitrary number of arguments and returns a return value
  168. // and/or an error.
  169. func (o *PtrObjectImpl) Call(_ ...Object) (ret Object, err error) {
  170. return nil, nil
  171. }
  172. // CanCall returns whether the Object can be Called.
  173. func (o *PtrObjectImpl) CanCall() bool {
  174. return false
  175. }
  176. // Array represents an array of objects.
  177. type Array struct {
  178. PtrObjectImpl
  179. Value []Object
  180. }
  181. // TypeName returns the name of the type.
  182. func (o *Array) TypeName() string {
  183. return "array"
  184. }
  185. func (o *Array) String() string {
  186. var elements []string
  187. for _, e := range o.Value {
  188. elements = append(elements, e.String())
  189. }
  190. return fmt.Sprintf("[%s]", strings.Join(elements, ", "))
  191. }
  192. // BinaryOp returns another object that is the result of a given binary
  193. // operator and a right-hand side object.
  194. func (o *Array) BinaryOp(op token.Token, rhs Object) (Object, error) {
  195. if rhs, ok := rhs.(*Array); ok {
  196. switch op {
  197. case token.Add:
  198. if len(rhs.Value) == 0 {
  199. return o, nil
  200. }
  201. return &Array{Value: append(o.Value, rhs.Value...)}, nil
  202. }
  203. }
  204. return nil, ErrInvalidOperator
  205. }
  206. // Copy returns a copy of the type.
  207. func (o *Array) Copy() Object {
  208. var c []Object
  209. for _, elem := range o.Value {
  210. c = append(c, elem.Copy())
  211. }
  212. return &Array{Value: c}
  213. }
  214. // IsFalsy returns true if the value of the type is falsy.
  215. func (o *Array) IsFalsy() bool {
  216. return len(o.Value) == 0
  217. }
  218. // Equals returns true if the value of the type is equal to the value of
  219. // another object.
  220. func (o *Array) Equals(x Object) bool {
  221. var xVal []Object
  222. switch x := x.(type) {
  223. case *Array:
  224. xVal = x.Value
  225. case *ImmutableArray:
  226. xVal = x.Value
  227. default:
  228. return false
  229. }
  230. if len(o.Value) != len(xVal) {
  231. return false
  232. }
  233. for i, e := range o.Value {
  234. if !e.Equals(xVal[i]) {
  235. return false
  236. }
  237. }
  238. return true
  239. }
  240. // IndexGet returns an element at a given index.
  241. func (o *Array) IndexGet(index Object) (res Object, err error) {
  242. intIdx, ok := index.(Int)
  243. if !ok {
  244. err = ErrInvalidIndexType
  245. return
  246. }
  247. idxVal := int(intIdx.Value)
  248. if idxVal < 0 || idxVal >= len(o.Value) {
  249. res = UndefinedValue
  250. return
  251. }
  252. res = o.Value[idxVal]
  253. return
  254. }
  255. // IndexSet sets an element at a given index.
  256. func (o *Array) IndexSet(index, value Object) (err error) {
  257. intIdx, ok := ToInt(index)
  258. if !ok {
  259. err = ErrInvalidIndexType
  260. return
  261. }
  262. if intIdx < 0 || intIdx >= len(o.Value) {
  263. err = ErrIndexOutOfBounds
  264. return
  265. }
  266. o.Value[intIdx] = value
  267. return nil
  268. }
  269. // Iterate creates an array iterator.
  270. func (o *Array) Iterate() Iterator {
  271. return &ArrayIterator{
  272. v: o.Value,
  273. l: len(o.Value),
  274. }
  275. }
  276. // CanIterate returns whether the Object can be Iterated.
  277. func (o *Array) CanIterate() bool {
  278. return true
  279. }
  280. // Bool represents a boolean value.
  281. type Bool struct {
  282. ObjectImpl
  283. // this is intentionally non-public to force using objects.TrueValue and
  284. // FalseValue always
  285. value bool
  286. }
  287. func (o Bool) String() string {
  288. if o.value {
  289. return "true"
  290. }
  291. return "false"
  292. }
  293. // TypeName returns the name of the type.
  294. func (o Bool) TypeName() string {
  295. return "bool"
  296. }
  297. // Copy returns a copy of the type.
  298. func (o Bool) Copy() Object {
  299. return o
  300. }
  301. // IsFalsy returns true if the value of the type is falsy.
  302. func (o Bool) IsFalsy() bool {
  303. return !o.value
  304. }
  305. // Equals returns true if the value of the type is equal to the value of
  306. // another object.
  307. func (o Bool) Equals(x Object) bool {
  308. return o == x
  309. }
  310. // GobDecode decodes bool value from input bytes.
  311. func (o *Bool) GobDecode(b []byte) (err error) {
  312. o.value = b[0] == 1
  313. return
  314. }
  315. // GobEncode encodes bool values into bytes.
  316. func (o Bool) GobEncode() (b []byte, err error) {
  317. if o.value {
  318. b = []byte{1}
  319. } else {
  320. b = []byte{0}
  321. }
  322. return
  323. }
  324. // BuiltinFunction represents a builtin function.
  325. type BuiltinFunction struct {
  326. PtrObjectImpl
  327. Name string
  328. Value CallableFunc
  329. }
  330. // TypeName returns the name of the type.
  331. func (o *BuiltinFunction) TypeName() string {
  332. return "builtin-function:" + o.Name
  333. }
  334. func (o *BuiltinFunction) String() string {
  335. return "<builtin-function>"
  336. }
  337. // Copy returns a copy of the type.
  338. func (o *BuiltinFunction) Copy() Object {
  339. return &BuiltinFunction{Value: o.Value}
  340. }
  341. // Equals returns true if the value of the type is equal to the value of
  342. // another object.
  343. func (o *BuiltinFunction) Equals(_ Object) bool {
  344. return false
  345. }
  346. // Call executes a builtin function.
  347. func (o *BuiltinFunction) Call(args ...Object) (Object, error) {
  348. return o.Value(args...)
  349. }
  350. // CanCall returns whether the Object can be Called.
  351. func (o *BuiltinFunction) CanCall() bool {
  352. return true
  353. }
  354. // BuiltinModule is an importable module that's written in Go.
  355. type BuiltinModule struct {
  356. Attrs map[string]Object
  357. }
  358. // Import returns an immutable map for the module.
  359. func (m *BuiltinModule) Import(moduleName string) (interface{}, error) {
  360. return m.AsImmutableMap(moduleName), nil
  361. }
  362. // AsImmutableMap converts builtin module into an immutable map.
  363. func (m *BuiltinModule) AsImmutableMap(moduleName string) *ImmutableMap {
  364. attrs := make(map[string]Object, len(m.Attrs))
  365. for k, v := range m.Attrs {
  366. attrs[k] = v.Copy()
  367. }
  368. attrs["__module_name__"] = &String{Value: moduleName}
  369. return &ImmutableMap{Value: attrs}
  370. }
  371. // Bytes represents a byte array.
  372. type Bytes struct {
  373. PtrObjectImpl
  374. Value []byte
  375. }
  376. func (o *Bytes) String() string {
  377. return string(o.Value)
  378. }
  379. // TypeName returns the name of the type.
  380. func (o *Bytes) TypeName() string {
  381. return "bytes"
  382. }
  383. // BinaryOp returns another object that is the result of a given binary
  384. // operator and a right-hand side object.
  385. func (o *Bytes) BinaryOp(op token.Token, rhs Object) (Object, error) {
  386. switch op {
  387. case token.Add:
  388. switch rhs := rhs.(type) {
  389. case *Bytes:
  390. if len(o.Value)+len(rhs.Value) > MaxBytesLen {
  391. return nil, ErrBytesLimit
  392. }
  393. return &Bytes{Value: append(o.Value, rhs.Value...)}, nil
  394. }
  395. }
  396. return nil, ErrInvalidOperator
  397. }
  398. // Copy returns a copy of the type.
  399. func (o *Bytes) Copy() Object {
  400. return &Bytes{Value: append([]byte{}, o.Value...)}
  401. }
  402. // IsFalsy returns true if the value of the type is falsy.
  403. func (o *Bytes) IsFalsy() bool {
  404. return len(o.Value) == 0
  405. }
  406. // Equals returns true if the value of the type is equal to the value of
  407. // another object.
  408. func (o *Bytes) Equals(x Object) bool {
  409. t, ok := x.(*Bytes)
  410. if !ok {
  411. return false
  412. }
  413. return bytes.Equal(o.Value, t.Value)
  414. }
  415. // IndexGet returns an element (as Int) at a given index.
  416. func (o *Bytes) IndexGet(index Object) (res Object, err error) {
  417. intIdx, ok := index.(Int)
  418. if !ok {
  419. err = ErrInvalidIndexType
  420. return
  421. }
  422. idxVal := int(intIdx.Value)
  423. if idxVal < 0 || idxVal >= len(o.Value) {
  424. res = UndefinedValue
  425. return
  426. }
  427. res = Int{Value: int64(o.Value[idxVal])}
  428. return
  429. }
  430. // Iterate creates a bytes iterator.
  431. func (o *Bytes) Iterate() Iterator {
  432. return &BytesIterator{
  433. v: o.Value,
  434. l: len(o.Value),
  435. }
  436. }
  437. // CanIterate returns whether the Object can be Iterated.
  438. func (o *Bytes) CanIterate() bool {
  439. return true
  440. }
  441. // Char represents a character value.
  442. type Char struct {
  443. ObjectImpl
  444. Value rune
  445. }
  446. func (o Char) String() string {
  447. return string(o.Value)
  448. }
  449. // TypeName returns the name of the type.
  450. func (o Char) TypeName() string {
  451. return "char"
  452. }
  453. // BinaryOp returns another object that is the result of a given binary
  454. // operator and a right-hand side object.
  455. func (o Char) BinaryOp(op token.Token, rhs Object) (Object, error) {
  456. switch rhs := rhs.(type) {
  457. case Char:
  458. switch op {
  459. case token.Add:
  460. r := o.Value + rhs.Value
  461. if r == o.Value {
  462. return o, nil
  463. }
  464. return Char{Value: r}, nil
  465. case token.Sub:
  466. r := o.Value - rhs.Value
  467. if r == o.Value {
  468. return o, nil
  469. }
  470. return Char{Value: r}, nil
  471. case token.Less:
  472. if o.Value < rhs.Value {
  473. return TrueValue, nil
  474. }
  475. return FalseValue, nil
  476. case token.Greater:
  477. if o.Value > rhs.Value {
  478. return TrueValue, nil
  479. }
  480. return FalseValue, nil
  481. case token.LessEq:
  482. if o.Value <= rhs.Value {
  483. return TrueValue, nil
  484. }
  485. return FalseValue, nil
  486. case token.GreaterEq:
  487. if o.Value >= rhs.Value {
  488. return TrueValue, nil
  489. }
  490. return FalseValue, nil
  491. }
  492. case Int:
  493. switch op {
  494. case token.Add:
  495. r := o.Value + rune(rhs.Value)
  496. if r == o.Value {
  497. return o, nil
  498. }
  499. return Char{Value: r}, nil
  500. case token.Sub:
  501. r := o.Value - rune(rhs.Value)
  502. if r == o.Value {
  503. return o, nil
  504. }
  505. return Char{Value: r}, nil
  506. case token.Less:
  507. if int64(o.Value) < rhs.Value {
  508. return TrueValue, nil
  509. }
  510. return FalseValue, nil
  511. case token.Greater:
  512. if int64(o.Value) > rhs.Value {
  513. return TrueValue, nil
  514. }
  515. return FalseValue, nil
  516. case token.LessEq:
  517. if int64(o.Value) <= rhs.Value {
  518. return TrueValue, nil
  519. }
  520. return FalseValue, nil
  521. case token.GreaterEq:
  522. if int64(o.Value) >= rhs.Value {
  523. return TrueValue, nil
  524. }
  525. return FalseValue, nil
  526. }
  527. }
  528. return nil, ErrInvalidOperator
  529. }
  530. // Copy returns a copy of the type.
  531. func (o Char) Copy() Object {
  532. return Char{Value: o.Value}
  533. }
  534. // IsFalsy returns true if the value of the type is falsy.
  535. func (o Char) IsFalsy() bool {
  536. return o.Value == 0
  537. }
  538. // Equals returns true if the value of the type is equal to the value of
  539. // another object.
  540. func (o Char) Equals(x Object) bool {
  541. t, ok := x.(Char)
  542. if !ok {
  543. return false
  544. }
  545. return o.Value == t.Value
  546. }
  547. // CompiledFunction represents a compiled function.
  548. type CompiledFunction struct {
  549. PtrObjectImpl
  550. Instructions []byte
  551. NumLocals int // number of local variables (including function parameters)
  552. NumParameters int
  553. VarArgs bool
  554. SourceMap map[int]parser.Pos
  555. Free []*ObjectPtr
  556. }
  557. // TypeName returns the name of the type.
  558. func (o *CompiledFunction) TypeName() string {
  559. return "compiled-function"
  560. }
  561. func (o *CompiledFunction) String() string {
  562. return "<compiled-function>"
  563. }
  564. // Copy returns a copy of the type.
  565. func (o *CompiledFunction) Copy() Object {
  566. return &CompiledFunction{
  567. Instructions: append([]byte{}, o.Instructions...),
  568. NumLocals: o.NumLocals,
  569. NumParameters: o.NumParameters,
  570. VarArgs: o.VarArgs,
  571. Free: append([]*ObjectPtr{}, o.Free...), // DO NOT Copy() of elements; these are variable pointers
  572. }
  573. }
  574. // Equals returns true if the value of the type is equal to the value of
  575. // another object.
  576. func (o *CompiledFunction) Equals(_ Object) bool {
  577. return false
  578. }
  579. // SourcePos returns the source position of the instruction at ip.
  580. func (o *CompiledFunction) SourcePos(ip int) parser.Pos {
  581. for ip >= 0 {
  582. if p, ok := o.SourceMap[ip]; ok {
  583. return p
  584. }
  585. ip--
  586. }
  587. return parser.NoPos
  588. }
  589. // CanCall returns whether the Object can be Called.
  590. func (o *CompiledFunction) CanCall() bool {
  591. return true
  592. }
  593. // Error represents an error value.
  594. type Error struct {
  595. PtrObjectImpl
  596. Value Object
  597. }
  598. // TypeName returns the name of the type.
  599. func (o *Error) TypeName() string {
  600. return "error"
  601. }
  602. func (o *Error) String() string {
  603. if o.Value != nil {
  604. return fmt.Sprintf("error: %s", o.Value.String())
  605. }
  606. return "error"
  607. }
  608. // IsFalsy returns true if the value of the type is falsy.
  609. func (o *Error) IsFalsy() bool {
  610. return true // error is always false.
  611. }
  612. // Copy returns a copy of the type.
  613. func (o *Error) Copy() Object {
  614. return &Error{Value: o.Value.Copy()}
  615. }
  616. // Equals returns true if the value of the type is equal to the value of
  617. // another object.
  618. func (o *Error) Equals(x Object) bool {
  619. return o == x // pointer equality
  620. }
  621. // IndexGet returns an element at a given index.
  622. func (o *Error) IndexGet(index Object) (res Object, err error) {
  623. if strIdx, _ := ToString(index); strIdx != "value" {
  624. err = ErrInvalidIndexOnError
  625. return
  626. }
  627. res = o.Value
  628. return
  629. }
  630. // Float represents a floating point number value.
  631. type Float struct {
  632. ObjectImpl
  633. Value float64
  634. }
  635. func (o Float) String() string {
  636. return strconv.FormatFloat(o.Value, 'f', -1, 64)
  637. }
  638. // TypeName returns the name of the type.
  639. func (o Float) TypeName() string {
  640. return "float"
  641. }
  642. // BinaryOp returns another object that is the result of a given binary
  643. // operator and a right-hand side object.
  644. func (o Float) BinaryOp(op token.Token, rhs Object) (Object, error) {
  645. switch rhs := rhs.(type) {
  646. case Float:
  647. switch op {
  648. case token.Add:
  649. r := o.Value + rhs.Value
  650. if r == o.Value {
  651. return o, nil
  652. }
  653. return Float{Value: r}, nil
  654. case token.Sub:
  655. r := o.Value - rhs.Value
  656. if r == o.Value {
  657. return o, nil
  658. }
  659. return Float{Value: r}, nil
  660. case token.Mul:
  661. r := o.Value * rhs.Value
  662. if r == o.Value {
  663. return o, nil
  664. }
  665. return Float{Value: r}, nil
  666. case token.Quo:
  667. r := o.Value / rhs.Value
  668. if r == o.Value {
  669. return o, nil
  670. }
  671. return Float{Value: r}, nil
  672. case token.Less:
  673. if o.Value < rhs.Value {
  674. return TrueValue, nil
  675. }
  676. return FalseValue, nil
  677. case token.Greater:
  678. if o.Value > rhs.Value {
  679. return TrueValue, nil
  680. }
  681. return FalseValue, nil
  682. case token.LessEq:
  683. if o.Value <= rhs.Value {
  684. return TrueValue, nil
  685. }
  686. return FalseValue, nil
  687. case token.GreaterEq:
  688. if o.Value >= rhs.Value {
  689. return TrueValue, nil
  690. }
  691. return FalseValue, nil
  692. }
  693. case Int:
  694. switch op {
  695. case token.Add:
  696. r := o.Value + float64(rhs.Value)
  697. if r == o.Value {
  698. return o, nil
  699. }
  700. return Float{Value: r}, nil
  701. case token.Sub:
  702. r := o.Value - float64(rhs.Value)
  703. if r == o.Value {
  704. return o, nil
  705. }
  706. return Float{Value: r}, nil
  707. case token.Mul:
  708. r := o.Value * float64(rhs.Value)
  709. if r == o.Value {
  710. return o, nil
  711. }
  712. return Float{Value: r}, nil
  713. case token.Quo:
  714. r := o.Value / float64(rhs.Value)
  715. if r == o.Value {
  716. return o, nil
  717. }
  718. return Float{Value: r}, nil
  719. case token.Less:
  720. if o.Value < float64(rhs.Value) {
  721. return TrueValue, nil
  722. }
  723. return FalseValue, nil
  724. case token.Greater:
  725. if o.Value > float64(rhs.Value) {
  726. return TrueValue, nil
  727. }
  728. return FalseValue, nil
  729. case token.LessEq:
  730. if o.Value <= float64(rhs.Value) {
  731. return TrueValue, nil
  732. }
  733. return FalseValue, nil
  734. case token.GreaterEq:
  735. if o.Value >= float64(rhs.Value) {
  736. return TrueValue, nil
  737. }
  738. return FalseValue, nil
  739. }
  740. }
  741. return nil, ErrInvalidOperator
  742. }
  743. // Copy returns a copy of the type.
  744. func (o Float) Copy() Object {
  745. return Float{Value: o.Value}
  746. }
  747. // IsFalsy returns true if the value of the type is falsy.
  748. func (o Float) IsFalsy() bool {
  749. return math.IsNaN(o.Value)
  750. }
  751. // Equals returns true if the value of the type is equal to the value of
  752. // another object.
  753. func (o Float) Equals(x Object) bool {
  754. t, ok := x.(Float)
  755. if !ok {
  756. return false
  757. }
  758. return o.Value == t.Value
  759. }
  760. // ImmutableArray represents an immutable array of objects.
  761. type ImmutableArray struct {
  762. PtrObjectImpl
  763. Value []Object
  764. }
  765. // TypeName returns the name of the type.
  766. func (o *ImmutableArray) TypeName() string {
  767. return "immutable-array"
  768. }
  769. func (o *ImmutableArray) String() string {
  770. var elements []string
  771. for _, e := range o.Value {
  772. elements = append(elements, e.String())
  773. }
  774. return fmt.Sprintf("[%s]", strings.Join(elements, ", "))
  775. }
  776. // BinaryOp returns another object that is the result of a given binary
  777. // operator and a right-hand side object.
  778. func (o *ImmutableArray) BinaryOp(op token.Token, rhs Object) (Object, error) {
  779. if rhs, ok := rhs.(*ImmutableArray); ok {
  780. switch op {
  781. case token.Add:
  782. return &Array{Value: append(o.Value, rhs.Value...)}, nil
  783. }
  784. }
  785. return nil, ErrInvalidOperator
  786. }
  787. // Copy returns a copy of the type.
  788. func (o *ImmutableArray) Copy() Object {
  789. var c []Object
  790. for _, elem := range o.Value {
  791. c = append(c, elem.Copy())
  792. }
  793. return &Array{Value: c}
  794. }
  795. // IsFalsy returns true if the value of the type is falsy.
  796. func (o *ImmutableArray) IsFalsy() bool {
  797. return len(o.Value) == 0
  798. }
  799. // Equals returns true if the value of the type is equal to the value of
  800. // another object.
  801. func (o *ImmutableArray) Equals(x Object) bool {
  802. var xVal []Object
  803. switch x := x.(type) {
  804. case *Array:
  805. xVal = x.Value
  806. case *ImmutableArray:
  807. xVal = x.Value
  808. default:
  809. return false
  810. }
  811. if len(o.Value) != len(xVal) {
  812. return false
  813. }
  814. for i, e := range o.Value {
  815. if !e.Equals(xVal[i]) {
  816. return false
  817. }
  818. }
  819. return true
  820. }
  821. // IndexGet returns an element at a given index.
  822. func (o *ImmutableArray) IndexGet(index Object) (res Object, err error) {
  823. intIdx, ok := index.(Int)
  824. if !ok {
  825. err = ErrInvalidIndexType
  826. return
  827. }
  828. idxVal := int(intIdx.Value)
  829. if idxVal < 0 || idxVal >= len(o.Value) {
  830. res = UndefinedValue
  831. return
  832. }
  833. res = o.Value[idxVal]
  834. return
  835. }
  836. // Iterate creates an array iterator.
  837. func (o *ImmutableArray) Iterate() Iterator {
  838. return &ArrayIterator{
  839. v: o.Value,
  840. l: len(o.Value),
  841. }
  842. }
  843. // CanIterate returns whether the Object can be Iterated.
  844. func (o *ImmutableArray) CanIterate() bool {
  845. return true
  846. }
  847. // ImmutableMap represents an immutable map object.
  848. type ImmutableMap struct {
  849. PtrObjectImpl
  850. Value map[string]Object
  851. }
  852. // TypeName returns the name of the type.
  853. func (o *ImmutableMap) TypeName() string {
  854. return "immutable-map"
  855. }
  856. func (o *ImmutableMap) String() string {
  857. var pairs []string
  858. for k, v := range o.Value {
  859. pairs = append(pairs, fmt.Sprintf("%s: %s", k, v.String()))
  860. }
  861. return fmt.Sprintf("{%s}", strings.Join(pairs, ", "))
  862. }
  863. // Copy returns a copy of the type.
  864. func (o *ImmutableMap) Copy() Object {
  865. c := make(map[string]Object)
  866. for k, v := range o.Value {
  867. c[k] = v.Copy()
  868. }
  869. return &Map{Value: c}
  870. }
  871. // IsFalsy returns true if the value of the type is falsy.
  872. func (o *ImmutableMap) IsFalsy() bool {
  873. return len(o.Value) == 0
  874. }
  875. // IndexGet returns the value for the given key.
  876. func (o *ImmutableMap) IndexGet(index Object) (res Object, err error) {
  877. strIdx, ok := ToString(index)
  878. if !ok {
  879. err = ErrInvalidIndexType
  880. return
  881. }
  882. res, ok = o.Value[strIdx]
  883. if !ok {
  884. res = UndefinedValue
  885. }
  886. return
  887. }
  888. // Equals returns true if the value of the type is equal to the value of
  889. // another object.
  890. func (o *ImmutableMap) Equals(x Object) bool {
  891. var xVal map[string]Object
  892. switch x := x.(type) {
  893. case *Map:
  894. xVal = x.Value
  895. case *ImmutableMap:
  896. xVal = x.Value
  897. default:
  898. return false
  899. }
  900. if len(o.Value) != len(xVal) {
  901. return false
  902. }
  903. for k, v := range o.Value {
  904. tv := xVal[k]
  905. if !v.Equals(tv) {
  906. return false
  907. }
  908. }
  909. return true
  910. }
  911. // Iterate creates an immutable map iterator.
  912. func (o *ImmutableMap) Iterate() Iterator {
  913. var keys []string
  914. for k := range o.Value {
  915. keys = append(keys, k)
  916. }
  917. return &MapIterator{
  918. v: o.Value,
  919. k: keys,
  920. l: len(keys),
  921. }
  922. }
  923. // CanIterate returns whether the Object can be Iterated.
  924. func (o *ImmutableMap) CanIterate() bool {
  925. return true
  926. }
  927. // Int represents an integer value.
  928. type Int struct {
  929. ObjectImpl
  930. Value int64
  931. }
  932. func (o Int) String() string {
  933. return strconv.FormatInt(o.Value, 10)
  934. }
  935. // TypeName returns the name of the type.
  936. func (o Int) TypeName() string {
  937. return "int"
  938. }
  939. // BinaryOp returns another object that is the result of a given binary
  940. // operator and a right-hand side object.
  941. func (o Int) BinaryOp(op token.Token, rhs Object) (Object, error) {
  942. switch rhs := rhs.(type) {
  943. case Int:
  944. switch op {
  945. case token.Add:
  946. r := o.Value + rhs.Value
  947. if r == o.Value {
  948. return o, nil
  949. }
  950. return Int{Value: r}, nil
  951. case token.Sub:
  952. r := o.Value - rhs.Value
  953. if r == o.Value {
  954. return o, nil
  955. }
  956. return Int{Value: r}, nil
  957. case token.Mul:
  958. r := o.Value * rhs.Value
  959. if r == o.Value {
  960. return o, nil
  961. }
  962. return Int{Value: r}, nil
  963. case token.Quo:
  964. r := o.Value / rhs.Value
  965. if r == o.Value {
  966. return o, nil
  967. }
  968. return Int{Value: r}, nil
  969. case token.Rem:
  970. r := o.Value % rhs.Value
  971. if r == o.Value {
  972. return o, nil
  973. }
  974. return Int{Value: r}, nil
  975. case token.And:
  976. r := o.Value & rhs.Value
  977. if r == o.Value {
  978. return o, nil
  979. }
  980. return Int{Value: r}, nil
  981. case token.Or:
  982. r := o.Value | rhs.Value
  983. if r == o.Value {
  984. return o, nil
  985. }
  986. return Int{Value: r}, nil
  987. case token.Xor:
  988. r := o.Value ^ rhs.Value
  989. if r == o.Value {
  990. return o, nil
  991. }
  992. return Int{Value: r}, nil
  993. case token.AndNot:
  994. r := o.Value &^ rhs.Value
  995. if r == o.Value {
  996. return o, nil
  997. }
  998. return Int{Value: r}, nil
  999. case token.Shl:
  1000. r := o.Value << uint64(rhs.Value)
  1001. if r == o.Value {
  1002. return o, nil
  1003. }
  1004. return Int{Value: r}, nil
  1005. case token.Shr:
  1006. r := o.Value >> uint64(rhs.Value)
  1007. if r == o.Value {
  1008. return o, nil
  1009. }
  1010. return Int{Value: r}, nil
  1011. case token.Less:
  1012. if o.Value < rhs.Value {
  1013. return TrueValue, nil
  1014. }
  1015. return FalseValue, nil
  1016. case token.Greater:
  1017. if o.Value > rhs.Value {
  1018. return TrueValue, nil
  1019. }
  1020. return FalseValue, nil
  1021. case token.LessEq:
  1022. if o.Value <= rhs.Value {
  1023. return TrueValue, nil
  1024. }
  1025. return FalseValue, nil
  1026. case token.GreaterEq:
  1027. if o.Value >= rhs.Value {
  1028. return TrueValue, nil
  1029. }
  1030. return FalseValue, nil
  1031. }
  1032. case Float:
  1033. switch op {
  1034. case token.Add:
  1035. return Float{Value: float64(o.Value) + rhs.Value}, nil
  1036. case token.Sub:
  1037. return Float{Value: float64(o.Value) - rhs.Value}, nil
  1038. case token.Mul:
  1039. return Float{Value: float64(o.Value) * rhs.Value}, nil
  1040. case token.Quo:
  1041. return Float{Value: float64(o.Value) / rhs.Value}, nil
  1042. case token.Less:
  1043. if float64(o.Value) < rhs.Value {
  1044. return TrueValue, nil
  1045. }
  1046. return FalseValue, nil
  1047. case token.Greater:
  1048. if float64(o.Value) > rhs.Value {
  1049. return TrueValue, nil
  1050. }
  1051. return FalseValue, nil
  1052. case token.LessEq:
  1053. if float64(o.Value) <= rhs.Value {
  1054. return TrueValue, nil
  1055. }
  1056. return FalseValue, nil
  1057. case token.GreaterEq:
  1058. if float64(o.Value) >= rhs.Value {
  1059. return TrueValue, nil
  1060. }
  1061. return FalseValue, nil
  1062. }
  1063. case Char:
  1064. switch op {
  1065. case token.Add:
  1066. return Char{Value: rune(o.Value) + rhs.Value}, nil
  1067. case token.Sub:
  1068. return Char{Value: rune(o.Value) - rhs.Value}, nil
  1069. case token.Less:
  1070. if o.Value < int64(rhs.Value) {
  1071. return TrueValue, nil
  1072. }
  1073. return FalseValue, nil
  1074. case token.Greater:
  1075. if o.Value > int64(rhs.Value) {
  1076. return TrueValue, nil
  1077. }
  1078. return FalseValue, nil
  1079. case token.LessEq:
  1080. if o.Value <= int64(rhs.Value) {
  1081. return TrueValue, nil
  1082. }
  1083. return FalseValue, nil
  1084. case token.GreaterEq:
  1085. if o.Value >= int64(rhs.Value) {
  1086. return TrueValue, nil
  1087. }
  1088. return FalseValue, nil
  1089. }
  1090. }
  1091. return nil, ErrInvalidOperator
  1092. }
  1093. // Copy returns a copy of the type.
  1094. func (o Int) Copy() Object {
  1095. return Int{Value: o.Value}
  1096. }
  1097. // IsFalsy returns true if the value of the type is falsy.
  1098. func (o Int) IsFalsy() bool {
  1099. return o.Value == 0
  1100. }
  1101. // Equals returns true if the value of the type is equal to the value of
  1102. // another object.
  1103. func (o Int) Equals(x Object) bool {
  1104. t, ok := x.(Int)
  1105. if !ok {
  1106. return false
  1107. }
  1108. return o.Value == t.Value
  1109. }
  1110. // Map represents a map of objects.
  1111. type Map struct {
  1112. PtrObjectImpl
  1113. Value map[string]Object
  1114. }
  1115. // TypeName returns the name of the type.
  1116. func (o *Map) TypeName() string {
  1117. return "map"
  1118. }
  1119. func (o *Map) String() string {
  1120. var pairs []string
  1121. for k, v := range o.Value {
  1122. pairs = append(pairs, fmt.Sprintf("%s: %s", k, v.String()))
  1123. }
  1124. return fmt.Sprintf("{%s}", strings.Join(pairs, ", "))
  1125. }
  1126. // Copy returns a copy of the type.
  1127. func (o *Map) Copy() Object {
  1128. c := make(map[string]Object)
  1129. for k, v := range o.Value {
  1130. c[k] = v.Copy()
  1131. }
  1132. return &Map{Value: c}
  1133. }
  1134. // IsFalsy returns true if the value of the type is falsy.
  1135. func (o *Map) IsFalsy() bool {
  1136. return len(o.Value) == 0
  1137. }
  1138. // Equals returns true if the value of the type is equal to the value of
  1139. // another object.
  1140. func (o *Map) Equals(x Object) bool {
  1141. var xVal map[string]Object
  1142. switch x := x.(type) {
  1143. case *Map:
  1144. xVal = x.Value
  1145. case *ImmutableMap:
  1146. xVal = x.Value
  1147. default:
  1148. return false
  1149. }
  1150. if len(o.Value) != len(xVal) {
  1151. return false
  1152. }
  1153. for k, v := range o.Value {
  1154. tv := xVal[k]
  1155. if !v.Equals(tv) {
  1156. return false
  1157. }
  1158. }
  1159. return true
  1160. }
  1161. // IndexGet returns the value for the given key.
  1162. func (o *Map) IndexGet(index Object) (res Object, err error) {
  1163. strIdx, ok := ToString(index)
  1164. if !ok {
  1165. err = ErrInvalidIndexType
  1166. return
  1167. }
  1168. res, ok = o.Value[strIdx]
  1169. if !ok {
  1170. res = UndefinedValue
  1171. }
  1172. return
  1173. }
  1174. // IndexSet sets the value for the given key.
  1175. func (o *Map) IndexSet(index, value Object) (err error) {
  1176. strIdx, ok := ToString(index)
  1177. if !ok {
  1178. err = ErrInvalidIndexType
  1179. return
  1180. }
  1181. o.Value[strIdx] = value
  1182. return nil
  1183. }
  1184. // Iterate creates a map iterator.
  1185. func (o *Map) Iterate() Iterator {
  1186. var keys []string
  1187. for k := range o.Value {
  1188. keys = append(keys, k)
  1189. }
  1190. return &MapIterator{
  1191. v: o.Value,
  1192. k: keys,
  1193. l: len(keys),
  1194. }
  1195. }
  1196. // CanIterate returns whether the Object can be Iterated.
  1197. func (o *Map) CanIterate() bool {
  1198. return true
  1199. }
  1200. // ObjectPtr represents a free variable.
  1201. type ObjectPtr struct {
  1202. PtrObjectImpl
  1203. Value *Object
  1204. }
  1205. func (o *ObjectPtr) String() string {
  1206. return "free-var"
  1207. }
  1208. // TypeName returns the name of the type.
  1209. func (o *ObjectPtr) TypeName() string {
  1210. return "<free-var>"
  1211. }
  1212. // Copy returns a copy of the type.
  1213. func (o *ObjectPtr) Copy() Object {
  1214. return o
  1215. }
  1216. // IsFalsy returns true if the value of the type is falsy.
  1217. func (o *ObjectPtr) IsFalsy() bool {
  1218. return o.Value == nil
  1219. }
  1220. // Equals returns true if the value of the type is equal to the value of
  1221. // another object.
  1222. func (o *ObjectPtr) Equals(x Object) bool {
  1223. return o == x
  1224. }
  1225. // String represents a string value.
  1226. type String struct {
  1227. PtrObjectImpl
  1228. Value string
  1229. runeStr []rune
  1230. }
  1231. // TypeName returns the name of the type.
  1232. func (o *String) TypeName() string {
  1233. return "string"
  1234. }
  1235. func (o *String) String() string {
  1236. return strconv.Quote(o.Value)
  1237. }
  1238. // BinaryOp returns another object that is the result of a given binary
  1239. // operator and a right-hand side object.
  1240. func (o *String) BinaryOp(op token.Token, rhs Object) (Object, error) {
  1241. switch op {
  1242. case token.Add:
  1243. switch rhs := rhs.(type) {
  1244. case *String:
  1245. if len(o.Value)+len(rhs.Value) > MaxStringLen {
  1246. return nil, ErrStringLimit
  1247. }
  1248. return &String{Value: o.Value + rhs.Value}, nil
  1249. default:
  1250. rhsStr := rhs.String()
  1251. if len(o.Value)+len(rhsStr) > MaxStringLen {
  1252. return nil, ErrStringLimit
  1253. }
  1254. return &String{Value: o.Value + rhsStr}, nil
  1255. }
  1256. case token.Less:
  1257. switch rhs := rhs.(type) {
  1258. case *String:
  1259. if o.Value < rhs.Value {
  1260. return TrueValue, nil
  1261. }
  1262. return FalseValue, nil
  1263. }
  1264. case token.LessEq:
  1265. switch rhs := rhs.(type) {
  1266. case *String:
  1267. if o.Value <= rhs.Value {
  1268. return TrueValue, nil
  1269. }
  1270. return FalseValue, nil
  1271. }
  1272. case token.Greater:
  1273. switch rhs := rhs.(type) {
  1274. case *String:
  1275. if o.Value > rhs.Value {
  1276. return TrueValue, nil
  1277. }
  1278. return FalseValue, nil
  1279. }
  1280. case token.GreaterEq:
  1281. switch rhs := rhs.(type) {
  1282. case *String:
  1283. if o.Value >= rhs.Value {
  1284. return TrueValue, nil
  1285. }
  1286. return FalseValue, nil
  1287. }
  1288. }
  1289. return nil, ErrInvalidOperator
  1290. }
  1291. // IsFalsy returns true if the value of the type is falsy.
  1292. func (o *String) IsFalsy() bool {
  1293. return len(o.Value) == 0
  1294. }
  1295. // Copy returns a copy of the type.
  1296. func (o *String) Copy() Object {
  1297. return &String{Value: o.Value}
  1298. }
  1299. // Equals returns true if the value of the type is equal to the value of
  1300. // another object.
  1301. func (o *String) Equals(x Object) bool {
  1302. t, ok := x.(*String)
  1303. if !ok {
  1304. return false
  1305. }
  1306. return o.Value == t.Value
  1307. }
  1308. // IndexGet returns a character at a given index.
  1309. func (o *String) IndexGet(index Object) (res Object, err error) {
  1310. intIdx, ok := index.(Int)
  1311. if !ok {
  1312. err = ErrInvalidIndexType
  1313. return
  1314. }
  1315. idxVal := int(intIdx.Value)
  1316. if o.runeStr == nil {
  1317. o.runeStr = []rune(o.Value)
  1318. }
  1319. if idxVal < 0 || idxVal >= len(o.runeStr) {
  1320. res = UndefinedValue
  1321. return
  1322. }
  1323. res = Char{Value: o.runeStr[idxVal]}
  1324. return
  1325. }
  1326. // Iterate creates a string iterator.
  1327. func (o *String) Iterate() Iterator {
  1328. if o.runeStr == nil {
  1329. o.runeStr = []rune(o.Value)
  1330. }
  1331. return &StringIterator{
  1332. v: o.runeStr,
  1333. l: len(o.runeStr),
  1334. }
  1335. }
  1336. // CanIterate returns whether the Object can be Iterated.
  1337. func (o *String) CanIterate() bool {
  1338. return true
  1339. }
  1340. // Time represents a time value.
  1341. type Time struct {
  1342. PtrObjectImpl
  1343. Value time.Time
  1344. }
  1345. func (o *Time) String() string {
  1346. return o.Value.String()
  1347. }
  1348. // TypeName returns the name of the type.
  1349. func (o *Time) TypeName() string {
  1350. return "time"
  1351. }
  1352. // BinaryOp returns another object that is the result of a given binary
  1353. // operator and a right-hand side object.
  1354. func (o *Time) BinaryOp(op token.Token, rhs Object) (Object, error) {
  1355. switch rhs := rhs.(type) {
  1356. case Int:
  1357. switch op {
  1358. case token.Add: // time + int => time
  1359. if rhs.Value == 0 {
  1360. return o, nil
  1361. }
  1362. return &Time{Value: o.Value.Add(time.Duration(rhs.Value))}, nil
  1363. case token.Sub: // time - int => time
  1364. if rhs.Value == 0 {
  1365. return o, nil
  1366. }
  1367. return &Time{Value: o.Value.Add(time.Duration(-rhs.Value))}, nil
  1368. }
  1369. case *Time:
  1370. switch op {
  1371. case token.Sub: // time - time => int (duration)
  1372. return Int{Value: int64(o.Value.Sub(rhs.Value))}, nil
  1373. case token.Less: // time < time => bool
  1374. if o.Value.Before(rhs.Value) {
  1375. return TrueValue, nil
  1376. }
  1377. return FalseValue, nil
  1378. case token.Greater:
  1379. if o.Value.After(rhs.Value) {
  1380. return TrueValue, nil
  1381. }
  1382. return FalseValue, nil
  1383. case token.LessEq:
  1384. if o.Value.Equal(rhs.Value) || o.Value.Before(rhs.Value) {
  1385. return TrueValue, nil
  1386. }
  1387. return FalseValue, nil
  1388. case token.GreaterEq:
  1389. if o.Value.Equal(rhs.Value) || o.Value.After(rhs.Value) {
  1390. return TrueValue, nil
  1391. }
  1392. return FalseValue, nil
  1393. }
  1394. }
  1395. return nil, ErrInvalidOperator
  1396. }
  1397. // Copy returns a copy of the type.
  1398. func (o *Time) Copy() Object {
  1399. return &Time{Value: o.Value}
  1400. }
  1401. // IsFalsy returns true if the value of the type is falsy.
  1402. func (o *Time) IsFalsy() bool {
  1403. return o.Value.IsZero()
  1404. }
  1405. // Equals returns true if the value of the type is equal to the value of
  1406. // another object.
  1407. func (o *Time) Equals(x Object) bool {
  1408. t, ok := x.(*Time)
  1409. if !ok {
  1410. return false
  1411. }
  1412. return o.Value.Equal(t.Value)
  1413. }
  1414. // Undefined represents an undefined value.
  1415. type Undefined struct {
  1416. PtrObjectImpl
  1417. }
  1418. // TypeName returns the name of the type.
  1419. func (o *Undefined) TypeName() string {
  1420. return "undefined"
  1421. }
  1422. func (o *Undefined) String() string {
  1423. return "<undefined>"
  1424. }
  1425. // Copy returns a copy of the type.
  1426. func (o *Undefined) Copy() Object {
  1427. return o
  1428. }
  1429. // IsFalsy returns true if the value of the type is falsy.
  1430. func (o *Undefined) IsFalsy() bool {
  1431. return true
  1432. }
  1433. // Equals returns true if the value of the type is equal to the value of
  1434. // another object.
  1435. func (o *Undefined) Equals(x Object) bool {
  1436. return o == x
  1437. }
  1438. // IndexGet returns an element at a given index.
  1439. func (o *Undefined) IndexGet(_ Object) (Object, error) {
  1440. return UndefinedValue, nil
  1441. }
  1442. // Iterate creates a map iterator.
  1443. func (o *Undefined) Iterate() Iterator {
  1444. return o
  1445. }
  1446. // CanIterate returns whether the Object can be Iterated.
  1447. func (o *Undefined) CanIterate() bool {
  1448. return true
  1449. }
  1450. // Next returns true if there are more elements to iterate.
  1451. func (o *Undefined) Next() bool {
  1452. return false
  1453. }
  1454. // Key returns the key or index value of the current element.
  1455. func (o *Undefined) Key() Object {
  1456. return o
  1457. }
  1458. // Value returns the value of the current element.
  1459. func (o *Undefined) Value() Object {
  1460. return o
  1461. }
  1462. // UserFunction represents a user function.
  1463. type UserFunction struct {
  1464. PtrObjectImpl
  1465. Name string
  1466. Value CallableFunc
  1467. }
  1468. // TypeName returns the name of the type.
  1469. func (o *UserFunction) TypeName() string {
  1470. return "user-function:" + o.Name
  1471. }
  1472. func (o *UserFunction) String() string {
  1473. return "<user-function>"
  1474. }
  1475. // Copy returns a copy of the type.
  1476. func (o *UserFunction) Copy() Object {
  1477. return &UserFunction{Value: o.Value, Name: o.Name}
  1478. }
  1479. // Equals returns true if the value of the type is equal to the value of
  1480. // another object.
  1481. func (o *UserFunction) Equals(_ Object) bool {
  1482. return false
  1483. }
  1484. // Call invokes a user function.
  1485. func (o *UserFunction) Call(args ...Object) (Object, error) {
  1486. return o.Value(args...)
  1487. }
  1488. // CanCall returns whether the Object can be Called.
  1489. func (o *UserFunction) CanCall() bool {
  1490. return true
  1491. }