module.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. var Code = require("code.js");
  3. var Errors = require("errors.js");
  4. var Procedure = require("procedure.js");
  5. var Symbol = require("symbol.js");
  6. var Type = require("type.js");
  7. var AnyTypeProc = Type.Procedure.extend({
  8. init: function AnyTypeProc(){
  9. Type.Procedure.prototype.init.call("PROCEDURE: JS.var");
  10. },
  11. result: function(){return any;}
  12. });
  13. var anyProc = new AnyTypeProc();
  14. var AnyType = Type.Basic.extend({
  15. init: function AnyType(){
  16. Type.Basic.prototype.init.call(this, "JS.var");
  17. },
  18. findSymbol: function(){return this;},
  19. callGenerator: function(context, id){
  20. return new Procedure.CallGenerator(context, id, anyProc);
  21. }
  22. });
  23. var any = new AnyType();
  24. var doProcId = "do$";
  25. var varTypeId = "var$";
  26. var doProcSymbol = (function(){
  27. var description = "JS predefined procedure 'do'";
  28. var DoProcCallGenerator = Procedure.CallGenerator.extend({
  29. init: function DoProcCallGenerator(context, id, type){
  30. Procedure.CallGenerator.prototype.init.call(this, context, id, type);
  31. this.__code = undefined;
  32. },
  33. prolog: function(id){return "";},
  34. checkArgument: function(pos, e){
  35. var type = e.type();
  36. if (!(type instanceof Type.String))
  37. throw new Errors.Error(
  38. "string is expected as an argument of " + description
  39. + ", got " + type.description());
  40. this.__code = type.value();
  41. return Procedure.CallGenerator.prototype.checkArgument.call(this, pos, e);
  42. },
  43. epilog: function(){return "";},
  44. writeArgumentCode: function(){},
  45. callExpression: function(){
  46. return new Code.Expression(this.__code);
  47. }
  48. });
  49. var args = [new Procedure.Arg(undefined, false)];
  50. var ProcType = Type.Procedure.extend({
  51. init: function(){
  52. Type.Procedure.prototype.init.call(this, doProcId);
  53. },
  54. description: function(){return description;},
  55. args: function(){return args;},
  56. result: function(){return undefined;},
  57. callGenerator: function(context, id){
  58. return new DoProcCallGenerator(context, id, this);
  59. }
  60. });
  61. return new Symbol.Symbol(doProcId, new ProcType());
  62. })();
  63. var varTypeSymbol = function(){
  64. return new Symbol.Symbol(varTypeId, new Type.TypeId(any));
  65. }();
  66. var JSModule = Type.Module.extend({
  67. init: function Module$JSModule(){
  68. Type.Module.prototype.init.call(this);
  69. },
  70. name: function(){return "this";},
  71. findSymbol: function(id){
  72. return new Symbol.Found(
  73. id == doProcId ? doProcSymbol
  74. : id == varTypeId ? varTypeSymbol
  75. : new Symbol.Symbol(id, any));
  76. }
  77. });
  78. exports.AnyType = AnyType;
  79. exports.JS = JSModule;