expr.c 15 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ctype.h>
  5. #include <mach.h>
  6. #define Extern extern
  7. #include "acid.h"
  8. static int fsize[] =
  9. {
  10. ['A'] 4,
  11. ['B'] 4,
  12. ['C'] 1,
  13. ['D'] 4,
  14. ['F'] 8,
  15. ['G'] 8,
  16. ['O'] 4,
  17. ['Q'] 4,
  18. ['R'] 4,
  19. ['S'] 4,
  20. ['U'] 4,
  21. ['V'] 8,
  22. ['X'] 4,
  23. ['Y'] 8,
  24. ['W'] 8,
  25. ['Z'] 8,
  26. ['a'] 4,
  27. ['b'] 1,
  28. ['c'] 1,
  29. ['d'] 2,
  30. ['f'] 4,
  31. ['g'] 4,
  32. ['o'] 2,
  33. ['q'] 2,
  34. ['r'] 2,
  35. ['s'] 4,
  36. ['u'] 2,
  37. ['x'] 2,
  38. };
  39. int
  40. fmtsize(Value *v)
  41. {
  42. int ret;
  43. switch(v->fmt) {
  44. default:
  45. return fsize[v->fmt];
  46. case 'i':
  47. case 'I':
  48. if(v->type != TINT || machdata == 0)
  49. error("no size for i fmt pointer ++/--");
  50. ret = (*machdata->instsize)(cormap, v->ival);
  51. if(ret < 0) {
  52. ret = (*machdata->instsize)(symmap, v->ival);
  53. if(ret < 0)
  54. error("%r");
  55. }
  56. return ret;
  57. }
  58. }
  59. void
  60. chklval(Node *lp)
  61. {
  62. if(lp->op != ONAME)
  63. error("need l-value");
  64. }
  65. void
  66. olist(Node *n, Node *res)
  67. {
  68. expr(n->left, res);
  69. expr(n->right, res);
  70. }
  71. void
  72. oeval(Node *n, Node *res)
  73. {
  74. expr(n->left, res);
  75. if(res->type != TCODE)
  76. error("bad type for eval");
  77. expr(res->cc, res);
  78. }
  79. void
  80. ocast(Node *n, Node *res)
  81. {
  82. if(n->sym->lt == 0)
  83. error("%s is not a complex type", n->sym->name);
  84. expr(n->left, res);
  85. res->comt = n->sym->lt;
  86. res->fmt = 'a';
  87. }
  88. void
  89. oindm(Node *n, Node *res)
  90. {
  91. Map *m;
  92. Node l;
  93. m = cormap;
  94. if(m == 0)
  95. m = symmap;
  96. expr(n->left, &l);
  97. if(l.type != TINT)
  98. error("bad type for *");
  99. if(m == 0)
  100. error("no map for *");
  101. indir(m, l.ival, l.fmt, res);
  102. res->comt = l.comt;
  103. }
  104. void
  105. oindc(Node *n, Node *res)
  106. {
  107. Map *m;
  108. Node l;
  109. m = symmap;
  110. if(m == 0)
  111. m = cormap;
  112. expr(n->left, &l);
  113. if(l.type != TINT)
  114. error("bad type for @");
  115. if(m == 0)
  116. error("no map for @");
  117. indir(m, l.ival, l.fmt, res);
  118. res->comt = l.comt;
  119. }
  120. void
  121. oframe(Node *n, Node *res)
  122. {
  123. char *p;
  124. Node *lp;
  125. long ival;
  126. Frtype *f;
  127. p = n->sym->name;
  128. while(*p && *p == '$')
  129. p++;
  130. lp = n->left;
  131. if(localaddr(cormap, p, lp->sym->name, &ival, rget) < 0)
  132. error("colon: %r");
  133. res->ival = ival;
  134. res->op = OCONST;
  135. res->fmt = 'X';
  136. res->type = TINT;
  137. /* Try and set comt */
  138. for(f = n->sym->local; f; f = f->next) {
  139. if(f->var == lp->sym) {
  140. res->comt = f->type;
  141. res->fmt = 'a';
  142. break;
  143. }
  144. }
  145. }
  146. void
  147. oindex(Node *n, Node *res)
  148. {
  149. Node l, r;
  150. expr(n->left, &l);
  151. expr(n->right, &r);
  152. if(r.type != TINT)
  153. error("bad type for []");
  154. switch(l.type) {
  155. default:
  156. error("lhs[] has bad type");
  157. case TINT:
  158. indir(cormap, l.ival+(r.ival*fsize[l.fmt]), l.fmt, res);
  159. res->comt = l.comt;
  160. res->fmt = l.fmt;
  161. break;
  162. case TLIST:
  163. nthelem(l.l, r.ival, res);
  164. break;
  165. case TSTRING:
  166. res->ival = 0;
  167. if(r.ival >= 0 && r.ival < l.string->len) {
  168. int xx8; /* to get around bug in vc */
  169. xx8 = r.ival;
  170. res->ival = l.string->string[xx8];
  171. }
  172. res->op = OCONST;
  173. res->type = TINT;
  174. res->fmt = 'c';
  175. break;
  176. }
  177. }
  178. void
  179. oappend(Node *n, Node *res)
  180. {
  181. Node r, l;
  182. expr(n->left, &l);
  183. expr(n->right, &r);
  184. if(l.type != TLIST)
  185. error("must append to list");
  186. append(res, &l, &r);
  187. }
  188. void
  189. odelete(Node *n, Node *res)
  190. {
  191. Node l, r;
  192. expr(n->left, &l);
  193. expr(n->right, &r);
  194. if(l.type != TLIST)
  195. error("must delete from list");
  196. if(r.type != TINT)
  197. error("delete index must be integer");
  198. delete(l.l, r.ival, res);
  199. }
  200. void
  201. ohead(Node *n, Node *res)
  202. {
  203. Node l;
  204. expr(n->left, &l);
  205. if(l.type != TLIST)
  206. error("head needs list");
  207. res->op = OCONST;
  208. if(l.l) {
  209. res->type = l.l->type;
  210. res->Store = l.l->Store;
  211. }
  212. else {
  213. res->type = TLIST;
  214. res->l = 0;
  215. }
  216. }
  217. void
  218. otail(Node *n, Node *res)
  219. {
  220. Node l;
  221. expr(n->left, &l);
  222. if(l.type != TLIST)
  223. error("tail needs list");
  224. res->op = OCONST;
  225. res->type = TLIST;
  226. if(l.l)
  227. res->l = l.l->next;
  228. else
  229. res->l = 0;
  230. }
  231. void
  232. oconst(Node *n, Node *res)
  233. {
  234. res->op = OCONST;
  235. res->type = n->type;
  236. res->Store = n->Store;
  237. res->comt = n->comt;
  238. }
  239. void
  240. oname(Node *n, Node *res)
  241. {
  242. Value *v;
  243. v = n->sym->v;
  244. if(v->set == 0)
  245. error("%s used but not set", n->sym->name);
  246. res->op = OCONST;
  247. res->type = v->type;
  248. res->Store = v->Store;
  249. res->comt = v->comt;
  250. }
  251. void
  252. octruct(Node *n, Node *res)
  253. {
  254. res->op = OCONST;
  255. res->type = TLIST;
  256. res->l = construct(n->left);
  257. }
  258. void
  259. oasgn(Node *n, Node *res)
  260. {
  261. Node *lp, r;
  262. Value *v;
  263. lp = n->left;
  264. switch(lp->op) {
  265. case OINDM:
  266. windir(cormap, lp->left, n->right, res);
  267. break;
  268. case OINDC:
  269. windir(symmap, lp->left, n->right, res);
  270. break;
  271. default:
  272. chklval(lp);
  273. v = lp->sym->v;
  274. expr(n->right, &r);
  275. v->set = 1;
  276. v->type = r.type;
  277. v->Store = r.Store;
  278. res->op = OCONST;
  279. res->type = v->type;
  280. res->Store = v->Store;
  281. res->comt = v->comt;
  282. }
  283. }
  284. void
  285. oadd(Node *n, Node *res)
  286. {
  287. Node l, r;
  288. expr(n->left, &l);
  289. expr(n->right, &r);
  290. res->fmt = l.fmt;
  291. res->op = OCONST;
  292. res->type = TFLOAT;
  293. switch(l.type) {
  294. default:
  295. error("bad lhs type +");
  296. case TINT:
  297. switch(r.type) {
  298. case TINT:
  299. res->type = TINT;
  300. res->ival = l.ival+r.ival;
  301. break;
  302. case TFLOAT:
  303. res->fval = l.ival+r.fval;
  304. break;
  305. default:
  306. error("bad rhs type +");
  307. }
  308. break;
  309. case TFLOAT:
  310. switch(r.type) {
  311. case TINT:
  312. res->fval = l.fval+r.ival;
  313. break;
  314. case TFLOAT:
  315. res->fval = l.fval+r.fval;
  316. break;
  317. default:
  318. error("bad rhs type +");
  319. }
  320. break;
  321. case TSTRING:
  322. if(r.type == TSTRING) {
  323. res->type = TSTRING;
  324. res->fmt = 's';
  325. res->string = stradd(l.string, r.string);
  326. break;
  327. }
  328. if(r.type == TINT) {
  329. res->type = TSTRING;
  330. res->fmt = 's';
  331. res->string = straddrune(l.string, r.ival);
  332. break;
  333. }
  334. error("bad rhs for +");
  335. case TLIST:
  336. res->type = TLIST;
  337. switch(r.type) {
  338. case TLIST:
  339. res->l = addlist(l.l, r.l);
  340. break;
  341. default:
  342. r.left = 0;
  343. r.right = 0;
  344. res->l = addlist(l.l, construct(&r));
  345. break;
  346. }
  347. }
  348. }
  349. void
  350. osub(Node *n, Node *res)
  351. {
  352. Node l, r;
  353. expr(n->left, &l);
  354. expr(n->right, &r);
  355. res->fmt = l.fmt;
  356. res->op = OCONST;
  357. res->type = TFLOAT;
  358. switch(l.type) {
  359. default:
  360. error("bad lhs type -");
  361. case TINT:
  362. switch(r.type) {
  363. case TINT:
  364. res->type = TINT;
  365. res->ival = l.ival-r.ival;
  366. break;
  367. case TFLOAT:
  368. res->fval = l.ival-r.fval;
  369. break;
  370. default:
  371. error("bad rhs type -");
  372. }
  373. break;
  374. case TFLOAT:
  375. switch(r.type) {
  376. case TINT:
  377. res->fval = l.fval-r.ival;
  378. break;
  379. case TFLOAT:
  380. res->fval = l.fval-r.fval;
  381. break;
  382. default:
  383. error("bad rhs type -");
  384. }
  385. break;
  386. }
  387. }
  388. void
  389. omul(Node *n, Node *res)
  390. {
  391. Node l, r;
  392. expr(n->left, &l);
  393. expr(n->right, &r);
  394. res->fmt = l.fmt;
  395. res->op = OCONST;
  396. res->type = TFLOAT;
  397. switch(l.type) {
  398. default:
  399. error("bad lhs type *");
  400. case TINT:
  401. switch(r.type) {
  402. case TINT:
  403. res->type = TINT;
  404. res->ival = l.ival*r.ival;
  405. break;
  406. case TFLOAT:
  407. res->fval = l.ival*r.fval;
  408. break;
  409. default:
  410. error("bad rhs type *");
  411. }
  412. break;
  413. case TFLOAT:
  414. switch(r.type) {
  415. case TINT:
  416. res->fval = l.fval*r.ival;
  417. break;
  418. case TFLOAT:
  419. res->fval = l.fval*r.fval;
  420. break;
  421. default:
  422. error("bad rhs type *");
  423. }
  424. break;
  425. }
  426. }
  427. void
  428. odiv(Node *n, Node *res)
  429. {
  430. Node l, r;
  431. expr(n->left, &l);
  432. expr(n->right, &r);
  433. res->fmt = l.fmt;
  434. res->op = OCONST;
  435. res->type = TFLOAT;
  436. switch(l.type) {
  437. default:
  438. error("bad lhs type /");
  439. case TINT:
  440. switch(r.type) {
  441. case TINT:
  442. res->type = TINT;
  443. if(r.ival == 0)
  444. error("zero divide");
  445. res->ival = l.ival/r.ival;
  446. break;
  447. case TFLOAT:
  448. if(r.fval == 0)
  449. error("zero divide");
  450. res->fval = l.ival/r.fval;
  451. break;
  452. default:
  453. error("bad rhs type /");
  454. }
  455. break;
  456. case TFLOAT:
  457. switch(r.type) {
  458. case TINT:
  459. res->fval = l.fval/r.ival;
  460. break;
  461. case TFLOAT:
  462. res->fval = l.fval/r.fval;
  463. break;
  464. default:
  465. error("bad rhs type /");
  466. }
  467. break;
  468. }
  469. }
  470. void
  471. omod(Node *n, Node *res)
  472. {
  473. Node l, r;
  474. expr(n->left, &l);
  475. expr(n->right, &r);
  476. res->fmt = l.fmt;
  477. res->op = OCONST;
  478. res->type = TINT;
  479. if(l.type != TINT || r.type != TINT)
  480. error("bad expr type %");
  481. res->ival = l.ival%r.ival;
  482. }
  483. void
  484. olsh(Node *n, Node *res)
  485. {
  486. Node l, r;
  487. expr(n->left, &l);
  488. expr(n->right, &r);
  489. res->fmt = l.fmt;
  490. res->op = OCONST;
  491. res->type = TINT;
  492. if(l.type != TINT || r.type != TINT)
  493. error("bad expr type <<");
  494. res->ival = l.ival<<r.ival;
  495. }
  496. void
  497. orsh(Node *n, Node *res)
  498. {
  499. Node l, r;
  500. expr(n->left, &l);
  501. expr(n->right, &r);
  502. res->fmt = l.fmt;
  503. res->op = OCONST;
  504. res->type = TINT;
  505. if(l.type != TINT || r.type != TINT)
  506. error("bad expr type >>");
  507. res->ival = (unsigned)l.ival>>r.ival;
  508. }
  509. void
  510. olt(Node *n, Node *res)
  511. {
  512. Node l, r;
  513. expr(n->left, &l);
  514. expr(n->right, &r);
  515. res->fmt = l.fmt;
  516. res->op = OCONST;
  517. res->type = TINT;
  518. switch(l.type) {
  519. default:
  520. error("bad lhs type <");
  521. case TINT:
  522. switch(r.type) {
  523. case TINT:
  524. res->ival = l.ival < r.ival;
  525. break;
  526. case TFLOAT:
  527. res->ival = l.ival < r.fval;
  528. break;
  529. default:
  530. error("bad rhs type <");
  531. }
  532. break;
  533. case TFLOAT:
  534. switch(r.type) {
  535. case TINT:
  536. res->ival = l.fval < r.ival;
  537. break;
  538. case TFLOAT:
  539. res->ival = l.fval < r.fval;
  540. break;
  541. default:
  542. error("bad rhs type <");
  543. }
  544. break;
  545. }
  546. }
  547. void
  548. ogt(Node *n, Node *res)
  549. {
  550. Node l, r;
  551. expr(n->left, &l);
  552. expr(n->right, &r);
  553. res->fmt = 'D';
  554. res->op = OCONST;
  555. res->type = TINT;
  556. switch(l.type) {
  557. default:
  558. error("bad lhs type >");
  559. case TINT:
  560. switch(r.type) {
  561. case TINT:
  562. res->ival = l.ival > r.ival;
  563. break;
  564. case TFLOAT:
  565. res->ival = l.ival > r.fval;
  566. break;
  567. default:
  568. error("bad rhs type >");
  569. }
  570. break;
  571. case TFLOAT:
  572. switch(r.type) {
  573. case TINT:
  574. res->ival = l.fval > r.ival;
  575. break;
  576. case TFLOAT:
  577. res->ival = l.fval > r.fval;
  578. break;
  579. default:
  580. error("bad rhs type >");
  581. }
  582. break;
  583. }
  584. }
  585. void
  586. oleq(Node *n, Node *res)
  587. {
  588. Node l, r;
  589. expr(n->left, &l);
  590. expr(n->right, &r);
  591. res->fmt = 'D';
  592. res->op = OCONST;
  593. res->type = TINT;
  594. switch(l.type) {
  595. default:
  596. error("bad expr type <=");
  597. case TINT:
  598. switch(r.type) {
  599. case TINT:
  600. res->ival = l.ival <= r.ival;
  601. break;
  602. case TFLOAT:
  603. res->ival = l.ival <= r.fval;
  604. break;
  605. default:
  606. error("bad expr type <=");
  607. }
  608. break;
  609. case TFLOAT:
  610. switch(r.type) {
  611. case TINT:
  612. res->ival = l.fval <= r.ival;
  613. break;
  614. case TFLOAT:
  615. res->ival = l.fval <= r.fval;
  616. break;
  617. default:
  618. error("bad expr type <=");
  619. }
  620. break;
  621. }
  622. }
  623. void
  624. ogeq(Node *n, Node *res)
  625. {
  626. Node l, r;
  627. expr(n->left, &l);
  628. expr(n->right, &r);
  629. res->fmt = 'D';
  630. res->op = OCONST;
  631. res->type = TINT;
  632. switch(l.type) {
  633. default:
  634. error("bad lhs type >=");
  635. case TINT:
  636. switch(r.type) {
  637. case TINT:
  638. res->ival = l.ival >= r.ival;
  639. break;
  640. case TFLOAT:
  641. res->ival = l.ival >= r.fval;
  642. break;
  643. default:
  644. error("bad rhs type >=");
  645. }
  646. break;
  647. case TFLOAT:
  648. switch(r.type) {
  649. case TINT:
  650. res->ival = l.fval >= r.ival;
  651. break;
  652. case TFLOAT:
  653. res->ival = l.fval >= r.fval;
  654. break;
  655. default:
  656. error("bad rhs type >=");
  657. }
  658. break;
  659. }
  660. }
  661. void
  662. oeq(Node *n, Node *res)
  663. {
  664. Node l, r;
  665. expr(n->left, &l);
  666. expr(n->right, &r);
  667. res->fmt = 'D';
  668. res->op = OCONST;
  669. res->type = TINT;
  670. res->ival = 0;
  671. switch(l.type) {
  672. default:
  673. break;
  674. case TINT:
  675. switch(r.type) {
  676. case TINT:
  677. res->ival = l.ival == r.ival;
  678. break;
  679. case TFLOAT:
  680. res->ival = l.ival == r.fval;
  681. break;
  682. default:
  683. break;
  684. }
  685. break;
  686. case TFLOAT:
  687. switch(r.type) {
  688. case TINT:
  689. res->ival = l.fval == r.ival;
  690. break;
  691. case TFLOAT:
  692. res->ival = l.fval == r.fval;
  693. break;
  694. default:
  695. break;
  696. }
  697. break;
  698. case TSTRING:
  699. if(r.type == TSTRING) {
  700. res->ival = scmp(r.string, l.string);
  701. break;
  702. }
  703. break;
  704. case TLIST:
  705. if(r.type == TLIST) {
  706. res->ival = listcmp(l.l, r.l);
  707. break;
  708. }
  709. break;
  710. }
  711. if(n->op == ONEQ)
  712. res->ival = !res->ival;
  713. }
  714. void
  715. oland(Node *n, Node *res)
  716. {
  717. Node l, r;
  718. expr(n->left, &l);
  719. expr(n->right, &r);
  720. res->fmt = l.fmt;
  721. res->op = OCONST;
  722. res->type = TINT;
  723. if(l.type != TINT || r.type != TINT)
  724. error("bad expr type &");
  725. res->ival = l.ival&r.ival;
  726. }
  727. void
  728. oxor(Node *n, Node *res)
  729. {
  730. Node l, r;
  731. expr(n->left, &l);
  732. expr(n->right, &r);
  733. res->fmt = l.fmt;
  734. res->op = OCONST;
  735. res->type = TINT;
  736. if(l.type != TINT || r.type != TINT)
  737. error("bad expr type ^");
  738. res->ival = l.ival^r.ival;
  739. }
  740. void
  741. olor(Node *n, Node *res)
  742. {
  743. Node l, r;
  744. expr(n->left, &l);
  745. expr(n->right, &r);
  746. res->fmt = l.fmt;
  747. res->op = OCONST;
  748. res->type = TINT;
  749. if(l.type != TINT || r.type != TINT)
  750. error("bad expr type |");
  751. res->ival = l.ival|r.ival;
  752. }
  753. void
  754. ocand(Node *n, Node *res)
  755. {
  756. Node l, r;
  757. res->fmt = l.fmt;
  758. res->op = OCONST;
  759. res->type = TINT;
  760. res->ival = 0;
  761. expr(n->left, &l);
  762. if(bool(&l) == 0)
  763. return;
  764. expr(n->right, &r);
  765. if(bool(&r) == 0)
  766. return;
  767. res->ival = 1;
  768. }
  769. void
  770. onot(Node *n, Node *res)
  771. {
  772. Node l;
  773. res->op = OCONST;
  774. res->type = TINT;
  775. res->ival = 0;
  776. expr(n->left, &l);
  777. if(bool(&l) == 0)
  778. res->ival = 1;
  779. }
  780. void
  781. ocor(Node *n, Node *res)
  782. {
  783. Node l, r;
  784. res->op = OCONST;
  785. res->type = TINT;
  786. res->ival = 0;
  787. expr(n->left, &l);
  788. if(bool(&l)) {
  789. res->ival = 1;
  790. return;
  791. }
  792. expr(n->right, &r);
  793. if(bool(&r)) {
  794. res->ival = 1;
  795. return;
  796. }
  797. }
  798. void
  799. oeinc(Node *n, Node *res)
  800. {
  801. Value *v;
  802. chklval(n->left);
  803. v = n->left->sym->v;
  804. res->op = OCONST;
  805. res->type = v->type;
  806. switch(v->type) {
  807. case TINT:
  808. if(n->op == OEDEC)
  809. v->ival -= fmtsize(v);
  810. else
  811. v->ival += fmtsize(v);
  812. break;
  813. case TFLOAT:
  814. if(n->op == OEDEC)
  815. v->fval--;
  816. else
  817. v->fval++;
  818. break;
  819. default:
  820. error("bad type for pre --/++");
  821. }
  822. res->Store = v->Store;
  823. }
  824. void
  825. opinc(Node *n, Node *res)
  826. {
  827. Value *v;
  828. chklval(n->left);
  829. v = n->left->sym->v;
  830. res->op = OCONST;
  831. res->type = v->type;
  832. res->Store = v->Store;
  833. switch(v->type) {
  834. case TINT:
  835. if(n->op == OPDEC)
  836. v->ival -= fmtsize(v);
  837. else
  838. v->ival += fmtsize(v);
  839. break;
  840. case TFLOAT:
  841. if(n->op == OPDEC)
  842. v->fval--;
  843. else
  844. v->fval++;
  845. break;
  846. default:
  847. error("bad type for post --/++");
  848. }
  849. }
  850. void
  851. ocall(Node *n, Node *res)
  852. {
  853. Lsym *s;
  854. Rplace *rsav;
  855. res->op = OCONST; /* Default return value */
  856. res->type = TLIST;
  857. res->l = 0;
  858. chklval(n->left);
  859. s = n->left->sym;
  860. if(n->builtin && !s->builtin){
  861. error("no builtin %s", s->name);
  862. return;
  863. }
  864. if(s->builtin && (n->builtin || s->proc == 0)) {
  865. (*s->builtin)(res, n->right);
  866. return;
  867. }
  868. if(s->proc == 0)
  869. error("no function %s", s->name);
  870. rsav = ret;
  871. call(s->name, n->right, s->proc->left, s->proc->right, res);
  872. ret = rsav;
  873. }
  874. void
  875. ofmt(Node *n, Node *res)
  876. {
  877. expr(n->left, res);
  878. res->fmt = n->right->ival;
  879. }
  880. void
  881. owhat(Node *n, Node *res)
  882. {
  883. res->op = OCONST; /* Default return value */
  884. res->type = TLIST;
  885. res->l = 0;
  886. whatis(n->sym);
  887. }
  888. void (*expop[])(Node*, Node*) =
  889. {
  890. [ONAME] oname,
  891. [OCONST] oconst,
  892. [OMUL] omul,
  893. [ODIV] odiv,
  894. [OMOD] omod,
  895. [OADD] oadd,
  896. [OSUB] osub,
  897. [ORSH] orsh,
  898. [OLSH] olsh,
  899. [OLT] olt,
  900. [OGT] ogt,
  901. [OLEQ] oleq,
  902. [OGEQ] ogeq,
  903. [OEQ] oeq,
  904. [ONEQ] oeq,
  905. [OLAND] oland,
  906. [OXOR] oxor,
  907. [OLOR] olor,
  908. [OCAND] ocand,
  909. [OCOR] ocor,
  910. [OASGN] oasgn,
  911. [OINDM] oindm,
  912. [OEDEC] oeinc,
  913. [OEINC] oeinc,
  914. [OPINC] opinc,
  915. [OPDEC] opinc,
  916. [ONOT] onot,
  917. [OIF] 0,
  918. [ODO] 0,
  919. [OLIST] olist,
  920. [OCALL] ocall,
  921. [OCTRUCT] octruct,
  922. [OWHILE] 0,
  923. [OELSE] 0,
  924. [OHEAD] ohead,
  925. [OTAIL] otail,
  926. [OAPPEND] oappend,
  927. [ORET] 0,
  928. [OINDEX] oindex,
  929. [OINDC] oindc,
  930. [ODOT] odot,
  931. [OLOCAL] 0,
  932. [OFRAME] oframe,
  933. [OCOMPLEX] 0,
  934. [ODELETE] odelete,
  935. [OCAST] ocast,
  936. [OFMT] ofmt,
  937. [OEVAL] oeval,
  938. [OWHAT] owhat,
  939. };