import { CodeSection } from "../types/CodeSection"; import { StyleRule } from "../types/style/StyleRule"; import { ScriptDescriptor } from "../types/script/ScriptDescriptor"; import { ScriptLineDescriptor } from "../types/script/ScriptLineDescriptor"; import { ScriptReturnType } from "./types/ScriptReturnType"; import { ScriptReturnDescriptor } from "../types/script/ScriptReturnDescriptor"; import { replaceVariableNames, isAsyncCode } from "./functions"; import { HtmlElement } from "../types/html/HtmlElement"; import { Scope } from "./types/Scope"; /** * Interpret the parsed operations from the app outline and prepare it for rendering * 1. Build a list of names that are in use (global) * 2. Keep track of the last defined variable * 3. Replace {{variable}} placeholder with its true name (in assignments and references) */ export function interpret( sections: CodeSection[] ): CodeSection[] { //First, compile a list of reserved names that cannot be used in child scopes const scope: Scope = { functions: getFunctions( sections ), variables: [], styles: getStyleRuleSelectors( sections ) } return sections.map( section => { switch( section.type ) { case 'html': section.content = processHTML( section.content as HtmlElement ); break; case 'script': section.content = processScript( section.content as ScriptDescriptor, scope ); break; case 'style': section.content = processStyle( section.content as StyleRule ); break; } return section; }); } function processScript( section: ScriptDescriptor, scope: Scope ) { section = replaceVariableNames( section, scope ); section.isAsync = isAsyncCode( section ); return section; } function processStyle( section: StyleRule ) { return section; } function processHTML( section: HtmlElement ) { return section; } /** * Retrieve a list of stylesheet rule selectors */ function getStyleRuleSelectors( sections: CodeSection[] ): string[] { return sections .filter( s => s.type === 'style' ) .map( x => x.content as StyleRule ) .map( rule => rule.targets) .reduce( ( list: string[], targets: string[] ) => { return [ ...list, ...targets ]; }, [] ) as string[]; } /** * Retrieve a list of function names described */ function getFunctions( sections: CodeSection[] ) { return sections .filter( s => s.type === 'script' ) .map( x => x.content as ScriptDescriptor ); } /** * Sifts through an entire function descriptor, attempting to locate the intended return type of the function */ function getFunctionReturnType( descriptor: ScriptDescriptor ): ScriptReturnType { function checkForReturn( lines: ScriptLineDescriptor[] ): ScriptReturnType { let returnType = ScriptReturnType.Unknown; for( let i = 0; i < lines.length; i++ ) { const line = lines[i]; if( line.children ) { const subReturnType = checkForReturn( line.children ); if( subReturnType !== ScriptReturnType.Unknown ) { returnType = subReturnType; } } else { if( line.type === 'return' ) { const returnStatement = line as ScriptReturnDescriptor; if( [ 'true', 'false' ].some( x => x === returnStatement.value ) ) return ScriptReturnType.Boolean; if( !isNaN( returnStatement.value as any ) ) return ScriptReturnType.Number; if( typeof( returnStatement.value ) === 'string' ) return ScriptReturnType.String; return ScriptReturnType.Unknown; } } } return returnType; } return checkForReturn( descriptor.lines ); }