pytifa.py 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  1. """"
  2. * Python Type Inferencer and Flow Analyzer
  3. * TIFA
  4. *
  5. * Depends on Skulpt
  6. *
  7. * TIFA uses a number of simplifications of the Python language.
  8. * * Variables cannot change type
  9. * * Variables cannot be deleted
  10. * * Complex types have to be homogenous
  11. * * No introspection or reflective characteristics
  12. * * No dunder methods
  13. * * No closures (maybe?)
  14. * * You cannot write a variable out of scope
  15. * * You cannot read a mutable variable out of scope
  16. * * No multiple inheritance
  17. *
  18. * Additionally, it reads the following as issues:
  19. * * Cannot read a variable without having first written to it.
  20. * * Cannot rewrite a variable unless it has been read.
  21. *
  22. * @constructor
  23. * @this {Tifa}
  24. """
  25. def Tifa():
  26. NodeVisitor.apply(this, Array.prototype.slice.call(arguments))
  27. Tifa.prototype = new NodeVisitor()
  28. """"
  29. * Processes the AST of the given source code to generate a report.
  30. *
  31. * @param {string} code - The Python source code
  32. * @param {string} filename - Optional, the filename (defaults to __main__)
  33. """
  34. Tifa.prototype.processCode = def(code, filename):
  35. # Code
  36. this.source = code is not "" ? code.split("\n") : []
  37. filename = filenameor '__main__'
  38. # Attempt parsing - might fail!
  39. var parse, ast
  40. try:
  41. parse = Sk.parse(filename, code)
  42. ast = Sk.astFromParse(parse.cst, filename, parse.flags)
  43. except Exception as error:
  44. this.report = {"success": False,
  45. "error": error,
  46. "issues": {},
  47. "variables": {}}
  48. return this.report
  49. try:
  50. return this.processAst(ast)
  51. except Exception as error:
  52. this.report = {"success": False,
  53. "error": error,
  54. "issues": {},
  55. "variables": {}}
  56. return this.report
  57. """"
  58. * Given an AST, actually performs the type and flow analyses to return a
  59. * report.
  60. *
  61. * @constructor
  62. * @this {BlockPyEditor}
  63. * @param {Object} main - The main BlockPy instance
  64. * @param {HTMLElement} tag - The HTML object this is attached to.
  65. """
  66. Tifa.prototype.processAst = def(ast):
  67. # Unique Global IDs
  68. this.PathId = 0
  69. this.ScopeId = 0
  70. this.AstId = 0
  71. # Human readable names
  72. this.pathNames = ['*Module']
  73. this.scopeNames = ['*Module']
  74. this.astNames = []
  75. # Complete record of all Names
  76. this.scopeChain = [this.ScopeId]
  77. this.pathChain = [this.PathId]
  78. this.nameMap = {}
  79. this.nameMap[this.PathId] = {}
  80. # Initialize a new, empty report
  81. this.initializeReport()
  82. # Traverse every node
  83. this.visit(ast)
  84. this.report.variables = this.nameMap
  85. # Check afterwards
  86. this.finishScope()
  87. this.report.topLevelVariables = {}
  88. var mainPathVars = this.nameMap[this.pathChain[0]]
  89. for var fullName in mainPathVars:
  90. var splitName = fullName.split("/")
  91. if splitName.length == 2 and splitName[0] == this.scopeChain[0]:
  92. this.report.topLevelVariables[splitName[1]] = mainPathVars[fullName]
  93. return this.report
  94. Tifa.prototype.initializeReport = def():
  95. this.report= {
  96. success: True,
  97. variables: {},
  98. issues: {
  99. "Parser Failure": [], # Complete failure to parse the code
  100. "Unconnected blocks": [], # Any names with ____
  101. "Empty Body": [], # Any use of pass on its own
  102. "Unnecessary Pass": [], # Any use of pass
  103. "Unread variables": [], # A variable was not read after it was defined
  104. "Undefined variables": [], # A variable was read before it was defined
  105. "Possibly undefined variables": [], # A variable was read but was not defined in every branch
  106. "Overwritten variables": [], # A written variable was written to again before being read
  107. "Append to non-list": [], # Attempted to use the append method on a non-list
  108. "Used iteration list": [], #
  109. "Unused iteration variable": [], #
  110. "Non-list iterations": [], #
  111. "Empty iterations": [], #
  112. "Type changes": [], #
  113. "Iteration variable is iteration list": [], #
  114. "Unknown functions": [], #
  115. "Not a function": [], # Attempt to call non-function as function
  116. "Action after return": [],
  117. "Incompatible types": [], #
  118. "Return outside function": [], #
  119. "Read out of scope": [], #
  120. "Write out of scope": [], # Attempted to modify a variable in a higher scope
  121. "Aliased built-in": [], #
  122. "Method not in Type": [], # A method was used that didn't exist for that type
  123. "Submodule not found": [],
  124. "Module not found": []
  125. }
  126. }
  127. return this.report
  128. Tifa.prototype.reportIssue = def(issue, data):
  129. this.report.issues[issue].append(data)
  130. /*
  131. https://github.com/python/typeshed/blob/master/stdlib/3/builtins.pyi
  132. List
  133. append
  134. index
  135. */
  136. /*
  137. Important concepts:
  138. Assign
  139. AugAssign
  140. Import
  141. With
  142. Attribute lookup
  143. Scope:
  144. ID
  145. CallStack:
  146. Functions
  147. Lambdas
  148. Return/yield
  149. Ifs/While : Branch
  150. For loops
  151. Classes
  152. Try/except Exception as
  153. Break/continue
  154. PathId: int
  155. AstId: int
  156. ScopeId: int
  157. NameMap
  158. 0 (top) =>
  159. bid =>
  160. fully-scoped-name: State
  161. State:
  162. name: str
  163. type: Type
  164. set, read, overwrite: str {yes, no, maybe}
  165. trace: list of TraceNode
  166. TraceNode:
  167. type: Type
  168. set, read, overwrite: str {yes, no, maybe}
  169. Type:
  170. ast: str {Unknown, Num, Str, List, Tuple, Set, Dict, Bool, None}
  171. empty?: boolean
  172. literals?: boolean
  173. subtype?: Type
  174. keys?: Type
  175. values?: Type
  176. subtypes?: list of Type
  177. definition?: JS def object
  178. Report:
  179. success: bool
  180. issues: Issue => list of IssueData
  181. variables: State
  182. IssueData:
  183. name?: str
  184. scope?: str
  185. position?: int
  186. type?: Type
  187. */
  188. Tifa.prototype.visit = def(node) {
  189. # Actions after return?
  190. if this.scopeChain.length > 1:
  191. var returnState = this.findVariablesScope("*return")
  192. if returnState.exists and returnState.inScope:
  193. if returnState.state.set == "yes":
  194. this.reportIssue("Action after return", {"position": Tifa.locate(node)})
  195. # No? All good.
  196. this.astNames.append(node._astname)
  197. this.AstId += 1
  198. var result = NodeVisitor.prototype.visit.call(this, node)
  199. this.AstId -= 1
  200. this.astNames.pop()
  201. if result is Unset:
  202. return Tifa._UNKNOWN_TYPE()
  203. else:
  204. return result
  205. }
  206. Tifa.prototype.visit_Assign = def(node) {
  207. # Handle value
  208. var valueType = this.visit(node.value)
  209. # Handle targets
  210. this.visitList(node.targets)
  211. var position = Tifa.locate(node)
  212. var that = this
  213. # TODO: Properly handle assignments with subscripts
  214. this.walkTargets(node.targets, valueType, (def action(target, type):
  215. if target._astname is 'Name':
  216. that.storeVariable(target.id.v, type, position)
  217. elif target._astname == 'Tuple'or target._astname == 'List':
  218. for var i = 0, len = target.elts.length; i < len; i++:
  219. var elt = target.elts[i]
  220. var eltType = Tifa.indexSequenceType(type, i)
  221. action(elt, eltType)
  222. ))
  223. }
  224. Tifa.prototype.visit_AugAssign = def(node) {
  225. # Handle value
  226. var right = this.visit(node.value)
  227. # Handle target
  228. var left = this.visit(node.target)
  229. var name = this.identifyCaller(node.target)
  230. # Handle op
  231. var position = Tifa.locate(node)
  232. # Handle operation
  233. this.loadVariable(name, position)
  234. var opLookup = Tifa.VALID_BINOP_TYPES[node.op.name]
  235. if left.name == "Unknown"or right.name == "Unknown":
  236. return Tifa._UNKNOWN_TYPE()
  237. elif opLookup:
  238. opLookup = opLookup[left.name]
  239. if opLookup:
  240. opLookup = opLookup[right.name]
  241. if opLookup:
  242. var resultType = opLookup(left, right)
  243. this.storeVariable(name, resultType, position)
  244. return resultType
  245. this.reportIssue("Incompatible types",
  246. {"left": left, "right": right,
  247. "operation": node.op.name,
  248. "position": position})
  249. return Tifa._UNKNOWN_TYPE()
  250. }
  251. Tifa.prototype.visit_Import = def(node):
  252. # Handle names
  253. var position = Tifa.locate(node)
  254. for var i = 0, len = node.names.length; i < len; i++:
  255. var module = node.names[i]
  256. var asname = module.asname is None ? module.name : module.asname
  257. var moduleType = this.loadModule(module.name.v, position)
  258. this.storeVariable(asname.v, moduleType, position)
  259. Tifa.prototype.visit_ImportFrom = def(node):
  260. # Handle names
  261. var position = Tifa.locate(node)
  262. for var i = 0, len = node.names.length; i < len; i++:
  263. if node.module is None:
  264. var alias = node.names[i]
  265. var asname = alias.asname is None ? alias.name : alias.asname
  266. var moduleType = this.loadModule(alias.name.v, position)
  267. var nameType = this.loadBuiltinAttr(moduleType, None, alias.name.v, position)
  268. this.storeVariable(asname.v, nameType, position)
  269. else:
  270. var moduleName = node.module.v
  271. var alias = node.names[i]
  272. var asname = alias.asname is None ? alias.name : alias.asname
  273. var moduleType = this.loadModule(moduleName, position)
  274. var nameType = this.loadBuiltinAttr(moduleType, None, alias.name.v, position)
  275. this.storeVariable(asname.v, nameType, position)
  276. Tifa.prototype.visit_BinOp = def(node):
  277. # Handle left and right
  278. var left = this.visit(node.left)
  279. var right = this.visit(node.right)
  280. # Handle operation
  281. var opLookup = Tifa.VALID_BINOP_TYPES[node.op.name]
  282. if left.name == "Unknown"or right.name == "Unknown":
  283. return Tifa._UNKNOWN_TYPE()
  284. elif opLookup:
  285. opLookup = opLookup[left.name]
  286. if opLookup:
  287. opLookup = opLookup[right.name]
  288. if opLookup:
  289. return opLookup(left, right)
  290. this.reportIssue("Incompatible types",
  291. {"left": left, "right": right,
  292. "operation": node.op.name,
  293. "position": Tifa.locate(node)})
  294. return Tifa._UNKNOWN_TYPE()
  295. Tifa.prototype.visit_UnaryOp = def(node):
  296. # Handle operand
  297. var operand = this.visit(node.operand)
  298. if node.op.name == "Not":
  299. return Tifa._BOOL_TYPE()
  300. elif operand.name == "Unknown":
  301. return Tifa._UNKNOWN_TYPE()
  302. else:
  303. var opLookup = Tifa.VALID_UNARYOP_TYPES[node.op.name]
  304. if opLookup:
  305. opLookup = opLookup[operand.name]
  306. if opLookup:
  307. return opLookup(operand)
  308. return Tifa._UNKNOWN_TYPE()
  309. Tifa.prototype.visit_BoolOp = def(node):
  310. # Handle left and right
  311. var values = []
  312. for var i=0, len=node.values.length; i < len; i+= 1:
  313. values[i] = this.visit(node.values[i])
  314. # TODO: Truthiness is not supported! Probably need a Union type
  315. # Handle operation
  316. return Tifa._BOOL_TYPE()
  317. Tifa.prototype.visit_Compare = def(node):
  318. # Handle left and right
  319. var left = this.visit(node.left)
  320. var comparators = []
  321. for var i=0, len=node.comparators.length; i < len; i+= 1:
  322. comparators[i] = this.visit(node.comparators[i])
  323. # Handle ops
  324. for var i=0, len=comparators.length; i < len; i+= 1:
  325. var op = node.ops[i]
  326. var right = comparators[i]
  327. switch (op.name:
  328. case "Eq": case "NotEq": case "Is": case "IsNot":
  329. break
  330. case "Lt": case "LtE": case "GtE": case "Gt":
  331. if left.name not = right.name ||
  332. (left.name not = "Num" and left.name not = "Bool" &&
  333. left.name not = "Str" and left.name not = "List" &&
  334. left.name not = "Set" and left.name not = "Tuple" )) {
  335. this.reportIssue("Incompatible types",
  336. {"left": left, "right": right,
  337. "operation": node.op.name,
  338. "position": Tifa.locate(node)})
  339. }
  340. break
  341. case "In": case "NotIn":
  342. if right.name not = "Str" and right.name not = "List" &&
  343. right.name not = "Set" and right.name not = "Tuple" &&
  344. right.name not = "Dict") {
  345. this.reportIssue("Incompatible types",
  346. {"left": left, "right": right,
  347. "operation": node.op.name,
  348. "position": Tifa.locate(node)})
  349. }
  350. break
  351. }
  352. return Tifa._BOOL_TYPE()
  353. }
  354. Tifa.prototype.visit_Call = def(node):
  355. var position = Tifa.locate(node)
  356. # Handle func part (Name or Attribute)
  357. var functionType = this.visit(node.func)
  358. var calleeName = this.identifyCaller(node)
  359. # Handle args
  360. var arguments = []
  361. for var i = 0, len = node.args.length; i < len; i++:
  362. var arg = this.visit(node.args[i])
  363. arguments.append(arg)
  364. # Handle keywords
  365. # Handle starargs
  366. # Handle kwargs
  367. if functionType.name == 'Function':
  368. var result = functionType.definition(this, functionType, calleeName, arguments, position)
  369. return result
  370. this.reportIssue("Not a function", {"position": position})
  371. return Tifa._UNKNOWN_TYPE()
  372. Tifa.prototype.IfExp = def(node):
  373. # Visit the conditional
  374. this.visit(node.test)
  375. # Visit the body
  376. var body = this.visit(node.body)
  377. # Visit the orelse
  378. var orelse = this.visit(node.orelse)
  379. if body.name not = orelse.name:
  380. # TODO: Union type?
  381. return Tifa._UNKNOWN_TYPE()
  382. else:
  383. return body
  384. Tifa.prototype.visit_If = def(node):
  385. # Visit the conditional
  386. this.visit(node.test)
  387. # Visit the bodies
  388. var thisPathId = this.PathId
  389. this.PathId += 1
  390. var ifPathId = this.PathId
  391. this.pathNames.append(ifPathId+'i')
  392. this.pathChain.unshift(ifPathId)
  393. this.nameMap[ifPathId] = {}
  394. for var i = 0, len = node.body.length; i < len; i++:
  395. this.visit(node.body[i])
  396. this.pathNames.pop()
  397. this.pathChain.shift()
  398. this.PathId += 1
  399. var elsePathId = this.PathId
  400. this.pathNames.append(elsePathId+'e')
  401. this.pathChain.unshift(elsePathId)
  402. this.nameMap[elsePathId] = {}
  403. for var i = 0, len = node.orelse.length; i < len; i++:
  404. this.visit(node.orelse[i])
  405. this.pathNames.pop()
  406. this.pathChain.shift()
  407. # Combine two paths into one
  408. for var ifName in this.nameMap[ifPathId]:
  409. if ifName in this.nameMap[elsePathId]:
  410. var combined = Tifa.combineStates(this.nameMap[ifPathId][ifName],
  411. this.nameMap[elsePathId][ifName],
  412. Tifa.locate(node))
  413. else:
  414. var combined = Tifa.combineStates(this.nameMap[ifPathId][ifName], None, Tifa.locate(node))
  415. this.nameMap[thisPathId][ifName] = combined
  416. for var elseName in this.nameMap[elsePathId]:
  417. if not (ifName in this.nameMap[elsePathId]):
  418. var combined = Tifa.combineStates(this.nameMap[elsePathId][elseName],
  419. None,
  420. Tifa.locate(node))
  421. this.nameMap[thisPathId][elseName] = combined
  422. Tifa.prototype.visit_While = def(node):
  423. # Visit the conditional
  424. this.visit(node.test)
  425. # Visit the bodies
  426. var thisPathId = this.PathId
  427. this.PathId += 1
  428. var ifPathId = this.PathId
  429. this.pathNames.append(ifPathId+'i')
  430. this.pathChain.unshift(ifPathId)
  431. this.nameMap[ifPathId] = {}
  432. this.pathNames.pop()
  433. this.pathChain.shift()
  434. this.PathId += 1
  435. var elsePathId = this.PathId
  436. this.pathNames.append(elsePathId+'e')
  437. this.pathChain.unshift(elsePathId)
  438. this.nameMap[elsePathId] = {}
  439. for var i = 0, len = node.orelse.length; i < len; i++:
  440. this.visit(node.orelse[i])
  441. this.visit(node.test)
  442. this.pathNames.pop()
  443. this.pathChain.shift()
  444. # Combine two paths into one
  445. for var ifName in this.nameMap[ifPathId]:
  446. if ifName in this.nameMap[elsePathId]:
  447. var combined = Tifa.combineStates(this.nameMap[ifPathId][ifName],
  448. this.nameMap[elsePathId][ifName],
  449. Tifa.locate(node))
  450. else:
  451. var combined = Tifa.combineStates(this.nameMap[ifPathId][ifName], None, Tifa.locate(node))
  452. this.nameMap[thisPathId][ifName] = combined
  453. for var elseName in this.nameMap[elsePathId]:
  454. if not (ifName in this.nameMap[elsePathId]):
  455. var combined = Tifa.combineStates(this.nameMap[elsePathId][elseName],
  456. None,
  457. Tifa.locate(node))
  458. this.nameMap[thisPathId][elseName] = combined
  459. Tifa.prototype.visit_ListComp = def(node):
  460. # TODO: Handle comprehension scope
  461. var generators = node.generators
  462. for var i = 0, len = generators.length; i < len; i++:
  463. this.visit(generators[i])
  464. var elt = node.elt
  465. return Tifa._LIST_OF_TYPE(this.visit(elt))
  466. Tifa.prototype.visit_SetComp = def(node):
  467. # TODO: Handle comprehension scope
  468. var generators = node.generators
  469. for var i = 0, len = generators.length; i < len; i++:
  470. this.visit(generators[i])
  471. var elt = node.elt
  472. return Tifa._SET_OF_TYPE(this.visit(elt))
  473. Tifa.prototype.visit_SetComp = def(node):
  474. # TODO: Handle comprehension scope
  475. var generators = node.generators
  476. for var i = 0, len = generators.length; i < len; i++:
  477. this.visit(generators[i])
  478. var key = node.key
  479. var value = node.value
  480. return Tifa._DICT_OF_TYPE(this.visit(key), this.visit(value))
  481. Tifa.prototype.visit_GeneratorExp = def(node):
  482. # TODO: Handle comprehension scope
  483. var generators = node.generators
  484. for var i = 0, len = generators.length; i < len; i++:
  485. this.visit(generators[i])
  486. var elt = node.elt
  487. return Tifa._GENERATOR_OF_TYPE(this.visit(elt))
  488. Tifa.prototype.visit_comprehension = def(node):
  489. # Handle the iteration list
  490. var position = Tifa.locate(node)
  491. var iter = node.iter
  492. var iterType
  493. var iterListName = None
  494. if iter._astname is "Name":
  495. iterListName = iter.id.v
  496. if iterListName == "___":
  497. this.reportIssue("Unconnected blocks", {"position": position})
  498. var state = this.iterateVariable(iterListName, Tifa.locate(node))
  499. iterType = state.type
  500. else:
  501. iterType = this.visit(iter)
  502. if Tifa.isTypeEmptyList(iterType):
  503. this.reportIssue("Empty iterations", {"position": position, "name": iterListName})
  504. if not (Tifa.isTypeSequence(iterType)):
  505. this.reportIssue("Non-list iterations", {"position": position, "name": iterListName})
  506. var iterSubtype = None
  507. if iterType is not None:
  508. iterSubtype = Tifa.indexSequenceType(iterType, 0)
  509. # Handle the iteration variable
  510. var iterVariableName = None
  511. var that = this
  512. (def walkTarget(target, type):
  513. if target._astname is 'Name':
  514. if iterVariableName == None:
  515. iterVariableName = target.id.v
  516. that.storeIterVariable(target.id.v, type, position)
  517. elif target._astname == 'Tuple'or target._astname == 'List':
  518. for var i = 0, len = target.elts.length; i < len; i++:
  519. var elt = target.elts[i]
  520. var eltType = Tifa.indexSequenceType(type, i)
  521. walkTarget(elt, eltType, position)
  522. )(node.target, iterSubtype)
  523. if iterVariableName and iterListName and iterListName == iterVariableName:
  524. this.reportIssue("Iteration variable is iteration list",
  525. {"name": iterVariableName, "position": position})
  526. # Handle the bodies
  527. for var i = 0, len = node.ifs.length; i < len; i++:
  528. this.visit(node.ifs[i])
  529. Tifa.prototype.visit_For = def(node):
  530. # Handle the iteration list
  531. var position = Tifa.locate(node)
  532. var iter = node.iter
  533. var iterType
  534. var iterListName = None
  535. if iter._astname is "Name":
  536. iterListName = iter.id.v
  537. if iterListName == "___":
  538. this.reportIssue("Unconnected blocks", {"position": position})
  539. var state = this.iterateVariable(iterListName, Tifa.locate(node))
  540. iterType = state.type
  541. else:
  542. iterType = this.visit(iter)
  543. if Tifa.isTypeEmptyList(iterType):
  544. this.reportIssue("Empty iterations", {"position": position, "name": iterListName})
  545. if not (Tifa.isTypeSequence(iterType)):
  546. this.reportIssue("Non-list iterations", {"position": position, "name": iterListName})
  547. var iterSubtype = None
  548. if iterType is not None:
  549. iterSubtype = Tifa.indexSequenceType(iterType, 0)
  550. # Handle the iteration variable
  551. var iterVariableName = None
  552. var that = this
  553. (def walkTarget(target, type):
  554. if target._astname is 'Name':
  555. if iterVariableName == None:
  556. iterVariableName = target.id.v
  557. that.storeIterVariable(target.id.v, type, position)
  558. elif target._astname == 'Tuple'or target._astname == 'List':
  559. for var i = 0, len = target.elts.length; i < len; i++:
  560. var elt = target.elts[i]
  561. var eltType = Tifa.indexSequenceType(type, i)
  562. walkTarget(elt, eltType, position)
  563. )(node.target, iterSubtype)
  564. if iterVariableName and iterListName and iterListName == iterVariableName:
  565. this.reportIssue("Iteration variable is iteration list",
  566. {"name": iterVariableName, "position": position})
  567. # Handle the bodies
  568. for var i = 0, len = node.body.length; i < len; i++:
  569. this.visit(node.body[i])
  570. for var i = 0, len = node.orelse.length; i < len; i++:
  571. this.visit(node.orelse[i])
  572. Tifa.prototype.visit_ClassDef = def(node):
  573. var className = node.name.v
  574. var position = Tifa.locate(node)
  575. this.storeVariable(className, {"type": "Class"}, position)
  576. # TODO: Define a new scope definition that executes the body
  577. # TODO: find __init__, execute that
  578. this.generic_visit(node)
  579. Tifa.prototype.visit_FunctionDef = def(node):
  580. # Name
  581. var functionName = node.name.v
  582. var position = Tifa.locate(node)
  583. var state = this.storeVariable(functionName, {"name": "Function"}, position)
  584. var definitionsScope = this.scopeChain.slice(0)
  585. state.type.definition = def(analyzer, callType, callName, parameters, callPosition):
  586. # Manage scope
  587. analyzer.ScopeId += 1
  588. var oldScope = analyzer.scopeChain.slice(0)
  589. analyzer.scopeChain = definitionsScope.slice(0)
  590. analyzer.scopeChain.unshift(analyzer.ScopeId)
  591. # Process arguments
  592. var args = node.args.args
  593. for var i = 0; i < args.length; i++:
  594. var arg = args[i]
  595. var name = Sk.ffi.remapToJs(arg.id)
  596. var parameter = Tifa.copyType(parameters[i])
  597. analyzer.storeVariable(name, parameter, position)
  598. for var i = 0, len = node.body.length; i < len; i++:
  599. analyzer.visit(node.body[i])
  600. var returnState = analyzer.findVariablesScope("*return")
  601. var returnValue = Tifa._NONE_TYPE()
  602. if returnState.exists and returnState.inScope:
  603. returnValue = analyzer.loadVariable("*return", callPosition).type
  604. # Return scope
  605. analyzer.finishScope()
  606. analyzer.scopeChain.shift()
  607. analyzer.scopeChain = oldScope
  608. return returnValue
  609. return state.type
  610. Tifa.prototype.visit_Lambda = def(node):
  611. # Name
  612. var position = Tifa.locate(node)
  613. var definitionsScope = this.scopeChain.slice(0)
  614. var functionType = { "name": "Function"}
  615. functionType.definition = def(analyzer, callType, callName, parameters, callPosition):
  616. # Manage scope
  617. analyzer.ScopeId += 1
  618. var oldScope = analyzer.scopeChain.slice(0)
  619. analyzer.scopeChain = definitionsScope.slice(0)
  620. analyzer.scopeChain.unshift(analyzer.ScopeId)
  621. # Process arguments
  622. var args = node.args.args
  623. for var i = 0; i < args.length; i++:
  624. var arg = args[i]
  625. var name = Sk.ffi.remapToJs(arg.id)
  626. var parameter = Tifa.copyType(parameters[i])
  627. analyzer.storeVariable(name, parameter, position)
  628. var returnValue = analyzer.visit(node.body)
  629. # Return scope
  630. analyzer.finishScope()
  631. analyzer.scopeChain.shift()
  632. analyzer.scopeChain = oldScope
  633. return returnValue
  634. return functionType
  635. Tifa.prototype.visit_Return = def(node):
  636. if this.scopeChain.length == 1:
  637. this.reportIssue("Return outside function", {"position": Tifa.locate(node)})
  638. var valueType = this.visit(node.value)
  639. var position = Tifa.locate(node)
  640. this.returnVariable(valueType, position)
  641. Tifa.prototype.visit_Attribute = def(node):
  642. # Handle value
  643. var valueType = this.visit(node.value)
  644. # Handle attr
  645. var position = Tifa.locate(node)
  646. var attrType = this.loadBuiltinAttr(valueType, node.value, node.attr.v, position)
  647. # Handle ctx
  648. //TODO: Handling contexts
  649. return attrType
  650. Tifa.prototype.visit_Subscript = def(node):
  651. # Handle value
  652. var valueType = this.visit(node.value)
  653. # Handle slice
  654. switch (node.slice._astname:
  655. case "Index": return Tifa.indexSequenceType(valueType, 0)
  656. case "Slice": return valueType
  657. }
  658. Tifa.prototype.visit_Name = def(node):
  659. var name = node.id.v
  660. if name == "___":
  661. this.reportIssue("Unconnected blocks", {"position": Tifa.locate(node)})
  662. if node.ctx.prototype._astname is "Load":
  663. if name == "True"or name == "False":
  664. return Tifa._BOOL_TYPE()
  665. elif name == "None":
  666. return Tifa._NONE_TYPE()
  667. else:
  668. var variable = this.findVariablesScope(name)
  669. var builtin = Tifa.loadBuiltin(name)
  670. if not variable.exists and builtin:
  671. return builtin
  672. else:
  673. var state = this.loadVariable(name, Tifa.locate(node))
  674. return state.type
  675. else:
  676. var variable = this.findVariablesScope(name)
  677. if variable.exists:
  678. return variable.state.type
  679. else:
  680. return Tifa._UNKNOWN_TYPE()
  681. Tifa.prototype.visit_Num = def(node):
  682. return Tifa._NUM_TYPE()
  683. Tifa.prototype.visit_Bool = def(node):
  684. return Tifa._BOOL_TYPE()
  685. Tifa.prototype.visit_Str = def(node):
  686. return Tifa._STR_TYPE()
  687. Tifa.prototype.visit_List = def(node):
  688. var type = Tifa._LIST_TYPE()
  689. if node.elts.length == 0:
  690. type.empty = True
  691. else:
  692. type.empty = False
  693. # TODO: confirm homogenous subtype
  694. for var i = 0, len = node.elts.length; i < len; i++:
  695. type.subtype = this.visit(node.elts[i])
  696. return type
  697. Tifa.prototype.visit_Dict = def(node):
  698. var type = Tifa._DICT_TYPE()
  699. type.keys = Tifa._UNKNOWN_TYPE()
  700. type.values = Tifa._UNKNOWN_TYPE()
  701. if node.keys.length == 0:
  702. type.empty = True
  703. else:
  704. type.empty = False
  705. for var i = 0, len = node.keys.length; i < len; i++:
  706. type.keys = this.visit(node.keys[i])
  707. type.values = this.visit(node.values[i])
  708. return type
  709. Tifa.prototype.visit_Tuple = def(node):
  710. var type = Tifa._TUPLE_TYPE()
  711. if node.elts.length == 0:
  712. type.empty = True
  713. else:
  714. type.empty = False
  715. type.subtypes = []
  716. # TODO: confirm homogenous subtype
  717. for var i = 0, len = node.elts.length; i < len; i++:
  718. type.subtypes.append(this.visit(node.elts[i]))
  719. return type
  720. Tifa.prototype.visit_With = def(node):
  721. var typeValue = this.visit(node.context_expr),
  722. position = Tifa.locate(node),
  723. that = this
  724. this.visitList(node.optional_vars)
  725. (def walkTarget(target, type):
  726. if target._astname is 'Name':
  727. that.storeVariable(target.id.v, type, position)
  728. elif target._astname == 'Tuple'or target._astname == 'List':
  729. for var i = 0, len = target.elts.length; i < len; i++:
  730. var elt = target.elts[i]
  731. var eltType = Tifa.indexSequenceType(type, i)
  732. walkTarget(elt, eltType, position)
  733. )(node.optional_vars, typeValue)
  734. # Handle the bodies
  735. for var i = 0, len = node.body.length; i < len; i++:
  736. this.visit(node.body[i])
  737. Tifa.prototype.identifyCaller = def (node):
  738. switch (node._astname:
  739. case "Name":
  740. return node.id.v
  741. case "Call":
  742. return this.identifyCaller(node.func)
  743. case "Attribute": case "Subscript":
  744. return this.identifyCaller(node.value)
  745. default:
  746. return None
  747. }
  748. }
  749. Tifa.prototype.findVariableOutOfScope = def (name) {
  750. for var pathId in this.nameMap:
  751. var path = this.nameMap[pathId]
  752. for var fullName in path:
  753. var unscopedName = /[^/]*$/.exec(fullName)[0]
  754. if unscopedName == name:
  755. return {'exists': True, 'inScope': False, 'scopedName': unscopedName, 'state': path[fullName]}
  756. return {'exists': False, 'inScope': False, 'state': path[fullName]}
  757. }
  758. Tifa.prototype.findVariablesScope = def (name) {
  759. for var j = 0, slen = this.scopeChain.length; j < slen; j++:
  760. for var i = 0, plen = this.pathChain.length; i < plen; i++:
  761. var pathId = this.pathChain[i]
  762. var path = this.nameMap[pathId]
  763. var fullName = this.scopeChain.slice(j).join("/") + "/" + name
  764. if fullName in path:
  765. if j == 0:
  766. return {'exists': True, 'inScope': True, 'scopedName': fullName, 'state': path[fullName]}
  767. else:
  768. return {'exists': True, 'inScope': False, 'scopedName': fullName, 'state': path[fullName]}
  769. return {'exists': False}
  770. }
  771. Tifa.prototype.storeVariable = def(name, type, position):
  772. var fullName = this.scopeChain.join("/") + "/" + name
  773. var currentPath = this.pathChain[0]
  774. var variable = this.findVariablesScope(name)
  775. if not variable.exists:
  776. # Create a new instance of the variable on the current path
  777. newState = {'name': name, 'trace': [], 'type': type,
  778. 'read': 'no', 'set': 'yes', 'over': 'no'}
  779. this.nameMap[currentPath][fullName] = newState
  780. else:
  781. newState = Tifa.traceState(variable.state, "store")
  782. if not variable.inScope:
  783. this.reportIssue("Write out of scope",
  784. {'name': name, 'position':position})
  785. # Type change?
  786. if not Tifa.areTypesEqual(type, variable.state.type):
  787. this.reportIssue("Type changes",
  788. {'name': name, 'position':position,
  789. 'old': variable.state.type, 'new': type})
  790. newState.type = type
  791. # Overwritten?
  792. if variable.state.set == 'yes' and variable.state.read == 'no':
  793. newState.over = 'yes'
  794. else:
  795. newState.set = 'yes'
  796. newState.read = 'no'
  797. this.nameMap[currentPath][fullName] = newState
  798. return newState
  799. Tifa.prototype.storeIterVariable = def(name, type, position):
  800. var state = this.storeVariable(name, type, position)
  801. state.read = 'yes'
  802. return state
  803. Tifa.prototype.appendVariable = def(name, type, position):
  804. var state = this.storeVariable(name, type, position)
  805. return state
  806. Tifa.prototype.returnVariable = def(type, position):
  807. return this.storeVariable("*return", type, position)
  808. Tifa.prototype.loadVariable = def(name, position):
  809. var fullName = this.scopeChain.join("/") + "/" + name
  810. var currentPath = this.pathChain[0]
  811. var variable = this.findVariablesScope(name)
  812. var outOfScopeVar = this.findVariableOutOfScope(name)
  813. if not variable.exists:
  814. # Create a new instance of the variable on the current path
  815. if outOfScopeVar.exists:
  816. this.reportIssue("Read out of scope",
  817. {'name': name, 'position':position})
  818. else:
  819. this.reportIssue("Undefined variables",
  820. {'name': name, 'position':position})
  821. state = {'name': name, 'trace': [], 'type': Tifa._UNKNOWN_TYPE(),
  822. 'read': 'yes', 'set': 'no', 'over': 'no'}
  823. this.nameMap[currentPath][fullName] = state
  824. return state
  825. else:
  826. var newState = Tifa.traceState(variable.state, "load")
  827. if variable.state.set == 'no':
  828. this.reportIssue("Undefined variables",
  829. {'name': name, 'position':position})
  830. if variable.state.set == 'maybe':
  831. this.reportIssue("Possibly undefined variables",
  832. {'name': name, 'position':position})
  833. newState.read = 'yes'
  834. if not variable.inScope:
  835. this.nameMap[currentPath][variable.scopedName] = newState
  836. else:
  837. this.nameMap[currentPath][fullName] = newState
  838. return newState
  839. Tifa.prototype.iterateVariable = def(name, position):
  840. return this.loadVariable(name, position)
  841. Tifa.prototype.finishScope = def():
  842. var pathId = this.pathChain[0]
  843. for var name in this.nameMap[pathId]:
  844. if Tifa.sameScope(name, this.scopeChain):
  845. var state = this.nameMap[pathId][name]
  846. if state.over == 'yes':
  847. this.reportIssue('Overwritten variables',
  848. {'name': state.name, 'position': 0}) # TODO position
  849. if state.read == 'no':
  850. this.reportIssue('Unread variables',
  851. {'name': state.name, 'type': state.type})
  852. Tifa.prototype.walkTargets = def(targets, type, walker):
  853. for var i = 0, len = targets.length; i < len; i++:
  854. walker(targets[i], type)
  855. Tifa._BOOL_TYPE = def() { return {'name': 'Bool'} }
  856. Tifa._NUM_TYPE = def() { return {'name': 'Num'} }
  857. Tifa._MODULE_TYPE = def() { return {'name': 'Module', 'submodules': []} }
  858. Tifa._STR_TYPE = def() { return {'name': 'Str'} }
  859. Tifa._FILE_TYPE = def() { return {'name': 'File'} }
  860. Tifa._SET_TYPE = def() { return {'name': 'Str', "empty": False} }
  861. Tifa._LIST_TYPE = def(isEmpty) { return {'name': 'List', "empty": not not isEmpty} }
  862. Tifa._DICT_TYPE = def() { return {'name': 'Dict', "empty": False} }
  863. Tifa._GENERATOR_OF_TYPE = def(subtype) { return {'name': 'Generator', "empty": False, "subtype": subtype} }
  864. Tifa._LIST_OF_TYPE = def(subtype) { return {'name': 'List', "empty": False, "subtype": subtype} }
  865. Tifa._SET_OF_TYPE = def(subtype) { return {'name': 'Set', "empty": False, "subtype": subtype} }
  866. Tifa._DICT_OF_TYPE = def(keytype, valuetype) {
  867. return {'name': 'Set', "empty": False, "keys": keytype, "values": valuetype} }
  868. Tifa._TUPLE_TYPE = def() { return {'name': 'Tuple'} }
  869. Tifa._NONE_TYPE = def() { return {'name': 'None'} }
  870. Tifa._UNKNOWN_TYPE = def() { return {'name': '*Unknown'} }
  871. Tifa.VALID_BINOP_TYPES = {
  872. 'Add': {'Num': {'Num': Tifa._NUM_TYPE},
  873. 'Str' :{'Str': Tifa._STR_TYPE},
  874. 'List': {'List': Tifa.mergeTypes},
  875. 'Tuple': {'Tuple': Tifa.mergeTypes}},
  876. 'Sub': {'Num': {'Num': Tifa._NUM_TYPE},
  877. 'Set': {'Set': Tifa.mergeTypes}},
  878. 'Div': {'Num': {'Num': Tifa._NUM_TYPE}},
  879. 'FloorDiv': {'Num': {'Num': Tifa._NUM_TYPE}},
  880. 'Mult': {'Num': {'Num': Tifa._NUM_TYPE,
  881. 'Str': Tifa._STR_TYPE,
  882. 'List': (l, r) => r,
  883. 'Tuple': (l, r) => r},
  884. 'Str': {'Num': Tifa._STR_TYPE},
  885. 'List': {'Num': (l, r) => l},
  886. 'Tuple': {'Num': (l, r) => l}},
  887. 'Pow': {'Num': {'Num': Tifa._NUM_TYPE}},
  888. # Should we allow old-fashioned string interpolation?
  889. # Currently, I vote no because it makes the code harder and is bad form.
  890. 'Mod': {'Num': {'Num': Tifa._NUM_TYPE}},
  891. 'LShift': {'Num': {'Num': Tifa._NUM_TYPE}},
  892. 'RShift': {'Num': {'Num': Tifa._NUM_TYPE}},
  893. 'BitOr': {'Num': {'Num': Tifa._NUM_TYPE},
  894. 'Bool': {'Num': Tifa._NUM_TYPE,
  895. 'Bool': Tifa._BOOL_TYPE},
  896. 'Set': {'Set': Tifa.mergeTypes}},
  897. 'BitXor': {'Num': {'Num': Tifa._NUM_TYPE},
  898. 'Bool': {'Num': Tifa._NUM_TYPE,
  899. 'Bool': Tifa._BOOL_TYPE},
  900. 'Set': {'Set': Tifa.mergeTypes}},
  901. 'BitAnd': {'Num': {'Num': Tifa._NUM_TYPE},
  902. 'Bool': {'Num': Tifa._NUM_TYPE,
  903. 'Bool': Tifa._BOOL_TYPE},
  904. 'Set': {'Set': Tifa.mergeTypes}},
  905. }
  906. Tifa.VALID_UNARYOP_TYPES = {
  907. 'UAdd': {'Num': Tifa._NUM_TYPE},
  908. 'USub': {'Num': Tifa._NUM_TYPE},
  909. 'Invert': {'Num': Tifa._NUM_TYPE}
  910. }
  911. Tifa.locate = def(node):
  912. return {"column": node.col_offset, "line": node.lineno}
  913. Tifa.indexSequenceType= def(type, i):
  914. if type.name == "Tuple":
  915. return type.subtypes[i]
  916. elif type.name == "List":
  917. return type.subtype
  918. elif type.name == "Generator":
  919. return type.subtype
  920. elif type.name == "Str":
  921. return Tifa._STR_TYPE()
  922. elif type.name == "File":
  923. return Tifa._STR_TYPE()
  924. elif type.name == "Dict":
  925. return Tifa.keys
  926. else:
  927. return Tifa._UNKNOWN_TYPE()
  928. Tifa.areTypesEqual = def(left, right):
  929. if left is Noneor right is None:
  930. return False
  931. elif left.name is "Unknown"or right.type is "Unknown":
  932. return False
  933. elif left.name is "List" and right.name is "List":
  934. if left.emptyor right.empty:
  935. return True
  936. else:
  937. return Tifa.areTypesEqual(left.subtype, right.subtype)
  938. else:
  939. return left.name == right.name
  940. Tifa.isTypeEmptyList = def(type):
  941. return (type.name is "List" and type.empty)
  942. Tifa.isTypeSequence = def(type):
  943. return arrayContains(type.name, ["List", "Set", "Tuple", "Str", "File", "Dict"])
  944. Tifa.sameScope = def(fullName, scopeChain):
  945. var nameScopes = fullName.split("/").slice(0, -1)
  946. var checkingScopes = scopeChain.slice().reverse()
  947. if nameScopes.length not = checkingScopes.length:
  948. return False
  949. for var i = 0, len = checkingScopes.length; i < len; i++:
  950. if nameScopes[i] not = checkingScopes[i]:
  951. return False
  952. return True
  953. Tifa.combineStates = def(left, right, position):
  954. var state = {'name': left.name, 'trace': left.trace, 'type': left.type,
  955. 'read': left.read, 'set': left.set, 'over': left.over}
  956. if right == None:
  957. state.read = left.read == 'no' ? 'no' : 'maybe'
  958. state.set = left.set == 'no' ? 'no' : 'maybe'
  959. state.over = left.over == 'no' ? 'no' : 'maybe'
  960. else:
  961. if not Tifa.areTypesEqual(left.type, right.type):
  962. this.reportIssue("Type changes",
  963. {'name': left.name, 'position':position,
  964. 'old': left.type, 'new': right.type})
  965. state.read = Tifa.matchRSO(left.read, right.read)
  966. state.set = Tifa.matchRSO(left.set, right.set)
  967. state.over = Tifa.matchRSO(left.over, right.over)
  968. return state
  969. Tifa.matchRSO = def(left, right):
  970. if left == right:
  971. return left
  972. else:
  973. return "maybe"
  974. Tifa.traceState = def(state, method):
  975. var newState = {
  976. 'type': state.type, 'method': method, 'trace': [],//state.trace.slice(0),
  977. 'set': state.set, 'read': state.read, 'over': state.over,
  978. 'name': state.name
  979. }
  980. newState.trace.append(state)
  981. return newState
  982. """"
  983. * Correctly clones a type, returning mutable types unchanged.
  984. """
  985. Tifa.copyType = def(type):
  986. switch (type.name:
  987. # Immutable types:
  988. case "Str": return Tifa._STR_TYPE()
  989. case "Num": return Tifa._NUM_TYPE()
  990. case "Tuple": return Tifa._TUPLE_TYPE()
  991. # Mutable types:
  992. default: return type
  993. }
  994. Tifa.mergeTypes = def(left, right):
  995. # TODO: Check that lists/sets have the same subtypes
  996. switch (left.name:
  997. case "List": return (left.empty ? right.subtype : left.subtype)
  998. case "Set": return (left.empty ? right.subtype : left.subtype)
  999. case "Tuple": return left.subtypes.concat(right.subtypes)
  1000. }
  1001. Tifa.defineSupplier = def(returnType):
  1002. return { "name": "Function",
  1003. "definition": def (analyzer, type, name, args, position):
  1004. return returnType
  1005. }
  1006. Tifa.defineIdentity = def(returnType):
  1007. return { "name": "Function",
  1008. "definition": def (analyzer, type, name, args, position):
  1009. if args.length:
  1010. return args[0]
  1011. return Tifa._UNKNOWN_TYPE()
  1012. }
  1013. Tifa.defineFunction = def(definition):
  1014. return { "name": "Function", "definition": definition}
  1015. Tifa.loadBuiltin = def(name):
  1016. switch (name:
  1017. # Void functions
  1018. case "print": return Tifa.defineSupplier(Tifa._NONE_TYPE())
  1019. # Math functions
  1020. case "int": case "abs": case "float": case "len":
  1021. case "ord": case "pow": case "round": case "sum":
  1022. return Tifa.defineSupplier(Tifa._NUM_TYPE())
  1023. # Boolean functions
  1024. case "bool": case "all": case "any": case "isinstance":
  1025. return Tifa.defineSupplier(Tifa._BOOL_TYPE())
  1026. # String functions
  1027. case "input": case "str": case "chr": case "repr":
  1028. return Tifa.defineSupplier(Tifa._STR_TYPE())
  1029. # File functions
  1030. case "open":
  1031. return Tifa.defineSupplier(Tifa._FILE_TYPE())
  1032. # List functions
  1033. case "map": return Tifa.defineSupplier(Tifa._LIST_TYPE())
  1034. case "list":
  1035. return Tifa.defineFunction(
  1036. def (analyzer, functionType, callee, args, position):
  1037. var returnType = Tifa._LIST_TYPE()
  1038. if args.length:
  1039. returnType.subtype = Tifa.indexSequenceType(args[0], 0)
  1040. # TODO: Should inherit the emptiness too
  1041. returnType.empty = True
  1042. else:
  1043. returnType.empty = True
  1044. return returnType
  1045. )
  1046. # Set functions
  1047. case "set":
  1048. return Tifa.defineFunction(
  1049. def (analyzer, functionType, callee, args, position):
  1050. var returnType = Tifa._SET_TYPE()
  1051. if args.length:
  1052. returnType.subtype = Tifa.indexSequenceType(args[0], 0)
  1053. # TODO: Should inherit the emptiness too
  1054. returnType.empty = True
  1055. else:
  1056. returnType.empty = True
  1057. return returnType
  1058. )
  1059. # Dict functions
  1060. case "dict":
  1061. return Tifa.defineSupplier(Tifa._DICT_TYPE())
  1062. # Pass through
  1063. case "sorted": case "reversed": case "filter":
  1064. return Tifa.defineIdentity()
  1065. # Special functions
  1066. case "range": return Tifa.defineSupplier(Tifa._LIST_OF_TYPE(Tifa._NUM_TYPE()))
  1067. case "dir": return Tifa.defineSupplier(Tifa._LIST_OF_TYPE(Tifa._STR_TYPE()))
  1068. case "max": case "min":
  1069. return Tifa.defineFunction(
  1070. def (analyzer, functionType, callee, args, position):
  1071. if args.length:
  1072. return Tifa.indexSequenceType(args[0], 0)
  1073. return Tifa._UNKNOWN_TYPE()
  1074. )
  1075. case "zip":
  1076. return Tifa.defineFunction(
  1077. def (analyzer, functionType, callee, args, position):
  1078. if args.length:
  1079. var tupledTypes = Tifa._TUPLE_TYPE()
  1080. tupledTypes.subtypes = []
  1081. for var i=0, len=args.length; i<len; i+=1:
  1082. tupledTypes.subtypes[i] = Tifa.indexSequenceType(args[i],0)
  1083. return Tifa._LIST_OF_TYPE(tupledTypes)
  1084. else:
  1085. var emptyList = Tifa._LIST_TYPE(True)
  1086. return emptyList
  1087. )
  1088. }
  1089. }
  1090. Tifa.MODULES = {
  1091. 'matplotlib': {
  1092. 'name': 'Module',
  1093. 'submodules': {
  1094. 'pyplot': {
  1095. 'name': 'Module',
  1096. 'fields': {
  1097. 'plot': Tifa.defineSupplier(Tifa._NONE_TYPE()),
  1098. 'hist': Tifa.defineSupplier(Tifa._NONE_TYPE()),
  1099. 'scatter': Tifa.defineSupplier(Tifa._NONE_TYPE()),
  1100. 'show': Tifa.defineSupplier(Tifa._NONE_TYPE()),
  1101. 'xlabel': Tifa.defineSupplier(Tifa._NONE_TYPE()),
  1102. 'ylabel': Tifa.defineSupplier(Tifa._NONE_TYPE()),
  1103. 'title': Tifa.defineSupplier(Tifa._NONE_TYPE()),
  1104. }
  1105. }
  1106. }
  1107. },
  1108. 'pprint': {
  1109. 'name': "Module",
  1110. 'fields': {
  1111. 'pprint': Tifa.defineSupplier(Tifa._NONE_TYPE())
  1112. }
  1113. },
  1114. 'random': {
  1115. 'name': "Module",
  1116. 'fields': {
  1117. 'randint': Tifa.defineSupplier(Tifa._NUM_TYPE())
  1118. }
  1119. },
  1120. 'turtle': {
  1121. 'name': "Module",
  1122. 'fields': {
  1123. 'forward': Tifa.defineSupplier(Tifa._NONE_TYPE()),
  1124. 'backward': Tifa.defineSupplier(Tifa._NONE_TYPE()),
  1125. 'color': Tifa.defineSupplier(Tifa._NONE_TYPE()),
  1126. 'right': Tifa.defineSupplier(Tifa._NONE_TYPE()),
  1127. 'left': Tifa.defineSupplier(Tifa._NONE_TYPE()),
  1128. }
  1129. },
  1130. 'math': {
  1131. 'name': "Module",
  1132. 'fields': {
  1133. 'ceil': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1134. 'copysign': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1135. 'fabs': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1136. 'factorial': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1137. 'floor': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1138. 'fmod': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1139. 'frexp': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1140. 'fsum': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1141. 'gcd': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1142. 'isclose': Tifa.defineSupplier(Tifa._BOOL_TYPE()),
  1143. 'isfinite': Tifa.defineSupplier(Tifa._BOOL_TYPE()),
  1144. 'isinf': Tifa.defineSupplier(Tifa._BOOL_TYPE()),
  1145. 'isnan': Tifa.defineSupplier(Tifa._BOOL_TYPE()),
  1146. 'ldexp': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1147. 'modf': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1148. 'trunc': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1149. 'exp': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1150. 'expm1': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1151. 'log': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1152. 'log1p': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1153. 'log2': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1154. 'log10': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1155. 'pow': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1156. 'sqrt': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1157. 'acos': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1158. 'sin': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1159. 'cos': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1160. 'tan': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1161. 'asin': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1162. 'acos': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1163. 'atan': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1164. 'atan2': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1165. 'hypot': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1166. 'degrees': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1167. 'radians': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1168. 'sinh': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1169. 'cosh': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1170. 'tanh': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1171. 'asinh': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1172. 'acosh': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1173. 'atanh': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1174. 'erf': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1175. 'erfc': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1176. 'gamma': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1177. 'lgamma': Tifa.defineSupplier(Tifa._NUM_TYPE()),
  1178. 'pi': Tifa._NUM_TYPE(),
  1179. 'e': Tifa._NUM_TYPE(),
  1180. 'tau': Tifa._NUM_TYPE(),
  1181. 'inf': Tifa._NUM_TYPE(),
  1182. 'nan': Tifa._NUM_TYPE(),
  1183. }
  1184. }
  1185. }
  1186. Tifa.prototype.loadModule = def(chain, position) {
  1187. var moduleNames = chain.split('.')
  1188. if moduleNames[0] in Tifa.MODULES:
  1189. var baseModule = Tifa.MODULES[moduleNames[0]]
  1190. for var i=1, len=moduleNames.length; i<len; i++:
  1191. if baseModule.name == "Module" and
  1192. moduleNames[i] in baseModule.submodules:
  1193. baseModule = baseModule.submodules[moduleNames[i]]
  1194. else:
  1195. this.reportIssue("Submodule not found",
  1196. {"name": chain, "position": position})
  1197. return baseModule
  1198. else:
  1199. this.reportIssue("Module not found",
  1200. {"name": chain, "position": position})
  1201. return Tifa._MODULE_TYPE()
  1202. }
  1203. Tifa.prototype.loadBuiltinAttr = def(type, func, attr, position) {
  1204. switch (type.name:
  1205. case "File":
  1206. switch (attr:
  1207. case "close": return Tifa.defineFunction(
  1208. def (analyzer, functionType, callee, args, position): pass
  1209. )
  1210. case "read": return Tifa.defineSupplier(Tifa._STR_TYPE())
  1211. case "readlines": return Tifa.defineSupplier(Tifa._LIST_OF_TYPE(Tifa._STR_TYPE()))
  1212. }
  1213. break
  1214. case "List":
  1215. switch (attr:
  1216. case "append": return Tifa.defineFunction(
  1217. def (analyzer, functionType, callee, args, position):
  1218. type.empty = False
  1219. type.subtype = args[0]
  1220. if callee:
  1221. analyzer.appendVariable(callee, Tifa._LIST_OF_TYPE(type.subtype), position)
  1222. )
  1223. break
  1224. case "Dict":
  1225. switch (attr:
  1226. case "items": return Tifa.defineFunction(
  1227. def (analyzer, functionType, callee, args, position):
  1228. return Tifa._LIST_OF_TYPE({
  1229. 'name': 'Tuple', 'subtypes': [type.keys, type.values], 'empty': False
  1230. })
  1231. )
  1232. break
  1233. case "Module":
  1234. if attr in type.fields):
  1235. return type.fields[attr]
  1236. else:
  1237. return Tifa._UNKNOWN_TYPE()
  1238. break
  1239. case "Str":
  1240. switch (attr:
  1241. # Strings
  1242. case "capitalize": case "center": case "expandtabs":
  1243. case "join": case "ljust": case "lower":
  1244. case "lstrip": case "replace": case "rjust":
  1245. case "rstrip": case "strip": case "swapcase":
  1246. case "title": case "translate": case "upper":
  1247. case "zfill":
  1248. return Tifa.defineSupplier(Tifa._STR_TYPE())
  1249. # Numbers
  1250. case "count": case "find": case "index":
  1251. case "rfind": case "rindex":
  1252. return Tifa.defineSupplier(Tifa._NUM_TYPE())
  1253. //Booleans
  1254. case "endswith": case "isalnum": case "isalpha":
  1255. case "isdigit": case "islower": case "isspace":
  1256. case "istitle": case "isupper": case "startswith":
  1257. return Tifa.defineSupplier(Tifa._BOOL_TYPE())
  1258. # Lists
  1259. case "rsplit": case "split": case "splitlines":
  1260. return Tifa.defineSupplier(Tifa._LIST_OF_TYPE(Tifa._STR_TYPE()))
  1261. }
  1262. break
  1263. }
  1264. # Catch mistakes
  1265. if attr == "append") {
  1266. this.reportIssue('Append to non-list',
  1267. {'name': this.identifyCaller(func),
  1268. 'position': position,
  1269. 'type': type})
  1270. }
  1271. return Tifa._NONE_TYPE()
  1272. }