jsgen: prefix JS keywords with 'v_'
							parent
							
								
									8500c8885c
								
							
						
					
					
						commit
						8de6da01d6
					
				| 
						 | 
				
			
			@ -8,8 +8,12 @@ import term
 | 
			
		|||
import v.util
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	//TODO
 | 
			
		||||
	js_reserved = ['delete', 'const', 'let', 'var', 'function', 'continue', 'break', 'switch', 'for', 'in', 'of', 'instanceof', 'typeof', 'do']
 | 
			
		||||
	// https://ecma-international.org/ecma-262/#sec-reserved-words
 | 
			
		||||
	js_reserved = ['await', 'break', 'case', 'catch', 'class', 'const', 'continue', 'debugger',
 | 
			
		||||
		'default', 'delete', 'do', 'else', 'enum', 'export', 'extends', 'finally', 'for', 'function',
 | 
			
		||||
		'if', 'implements', 'import', 'in', 'instanceof', 'interface', 'let', 'new', 'package', 'private',
 | 
			
		||||
		'protected', 'public', 'return', 'static', 'super', 'switch', 'this', 'throw', 'try', 'typeof',
 | 
			
		||||
		'var', 'void', 'while', 'with', 'yield']
 | 
			
		||||
	tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t', '\t\t\t\t\t\t\t\t']
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -95,7 +99,6 @@ pub fn (g mut JsGen) enter_namespace(n string) {
 | 
			
		|||
		g.indents[g.namespace] = 0
 | 
			
		||||
		g.out.writeln('const $n = (function () {')
 | 
			
		||||
	}
 | 
			
		||||
	//
 | 
			
		||||
	else {
 | 
			
		||||
		g.out = g.namespaces[g.namespace]
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -107,10 +110,7 @@ pub fn (g mut JsGen) escape_namespace() {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
pub fn (g mut JsGen) push_pub_var(s string) {
 | 
			
		||||
	// Workaround until `m[key]<<val` works.
 | 
			
		||||
	mut arr := g.namespaces_pub[g.namespace]
 | 
			
		||||
	arr << s
 | 
			
		||||
	g.namespaces_pub[g.namespace] = arr
 | 
			
		||||
	g.namespaces_pub[g.namespace] << s
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn (g mut JsGen) find_class_methods(stmts []ast.Stmt) {
 | 
			
		||||
| 
						 | 
				
			
			@ -207,11 +207,11 @@ pub fn (g mut JsGen) gen_indent() {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
pub fn (g mut JsGen) inc_indent() {
 | 
			
		||||
	g.indents[g.namespace] = g.indents[g.namespace] + 1
 | 
			
		||||
	g.indents[g.namespace]++
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn (g mut JsGen) dec_indent() {
 | 
			
		||||
	g.indents[g.namespace] = g.indents[g.namespace] - 1
 | 
			
		||||
	g.indents[g.namespace]--
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn (g mut JsGen) write(s string) {
 | 
			
		||||
| 
						 | 
				
			
			@ -227,7 +227,16 @@ pub fn (g mut JsGen) writeln(s string) {
 | 
			
		|||
 | 
			
		||||
pub fn (g mut JsGen) new_tmp_var() string {
 | 
			
		||||
	g.tmp_count++
 | 
			
		||||
	return 'tmp$g.tmp_count'
 | 
			
		||||
	return '_tmp$g.tmp_count'
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
[inline]
 | 
			
		||||
fn js_name(name_ string) string {
 | 
			
		||||
	name := name_.replace('.', '__')
 | 
			
		||||
	if name in js_reserved {
 | 
			
		||||
		return 'v_$name'
 | 
			
		||||
	}
 | 
			
		||||
	return name
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn (g mut JsGen) stmts(stmts []ast.Stmt) {
 | 
			
		||||
| 
						 | 
				
			
			@ -299,7 +308,7 @@ fn (g mut JsGen) stmt(node ast.Stmt) {
 | 
			
		|||
			g.writeln('')
 | 
			
		||||
		}
 | 
			
		||||
		ast.GotoLabel {
 | 
			
		||||
			g.writeln('$it.name:')
 | 
			
		||||
			g.writeln('${js_name(it.name)}:')
 | 
			
		||||
		}
 | 
			
		||||
		ast.GotoStmt {
 | 
			
		||||
			// skip: JS has no goto
 | 
			
		||||
| 
						 | 
				
			
			@ -354,7 +363,7 @@ fn (g mut JsGen) expr(node ast.Expr) {
 | 
			
		|||
				// example: foo.bar.baz()
 | 
			
		||||
				g.write('.')
 | 
			
		||||
			}
 | 
			
		||||
			g.write('${it.name}(')
 | 
			
		||||
			g.write('${js_name(it.name)}(')
 | 
			
		||||
			for i, arg in it.args {
 | 
			
		||||
				g.expr(arg.expr)
 | 
			
		||||
				if i != it.args.len - 1 {
 | 
			
		||||
| 
						 | 
				
			
			@ -421,7 +430,7 @@ fn (g mut JsGen) expr(node ast.Expr) {
 | 
			
		|||
			g.gen_selector_expr(it)
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
			println(term.red('jsgen.expr(): bad node'))
 | 
			
		||||
			println(term.red('jsgen.expr(): bad node "${typeof(node)}"'))
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -522,7 +531,7 @@ fn (g mut JsGen) gen_assign_stmt(it ast.AssignStmt) {
 | 
			
		|||
			styp := g.typ(ident_var_info.typ)
 | 
			
		||||
			jsdoc.write(styp)
 | 
			
		||||
 | 
			
		||||
			stmt.write('$ident.name')
 | 
			
		||||
			stmt.write(js_name(ident.name))
 | 
			
		||||
 | 
			
		||||
			if i < it.left.len - 1 {
 | 
			
		||||
				jsdoc.write(', ')
 | 
			
		||||
| 
						 | 
				
			
			@ -564,7 +573,7 @@ fn (g mut JsGen) gen_assign_stmt(it ast.AssignStmt) {
 | 
			
		|||
				g.write('const ')
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			g.write('$ident.name = ')
 | 
			
		||||
			g.write('${js_name(ident.name)} = ')
 | 
			
		||||
			g.expr(val)
 | 
			
		||||
 | 
			
		||||
			if g.inside_loop {
 | 
			
		||||
| 
						 | 
				
			
			@ -604,7 +613,7 @@ fn (g mut JsGen) gen_const_decl(it ast.ConstDecl) {
 | 
			
		|||
		g.constants.write('\t')
 | 
			
		||||
		g.constants.writeln(g.doc.gen_typ(typ, field.name))
 | 
			
		||||
		g.constants.write('\t')
 | 
			
		||||
		g.constants.write('$field.name: $val')
 | 
			
		||||
		g.constants.write('${js_name(field.name)}: $val')
 | 
			
		||||
		if i < it.fields.len - 1 {
 | 
			
		||||
			g.constants.writeln(',')
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -621,7 +630,7 @@ fn (g mut JsGen) gen_defer_stmts() {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
fn (g mut JsGen) gen_enum_decl(it ast.EnumDecl) {
 | 
			
		||||
	g.writeln('const $it.name = Object.freeze({')
 | 
			
		||||
	g.writeln('const ${js_name(it.name)} = Object.freeze({')
 | 
			
		||||
	g.inc_indent()
 | 
			
		||||
	for i, field in it.fields {
 | 
			
		||||
		g.write('$field.name: ')
 | 
			
		||||
| 
						 | 
				
			
			@ -682,7 +691,7 @@ fn (g mut JsGen) gen_method_decl(it ast.FnDecl) {
 | 
			
		|||
		}
 | 
			
		||||
		g.write('function(')
 | 
			
		||||
	} else {
 | 
			
		||||
		mut name := it.name
 | 
			
		||||
		mut name := js_name(it.name)
 | 
			
		||||
		c := name[0]
 | 
			
		||||
		if c in [`+`, `-`, `*`, `/`] {
 | 
			
		||||
			name = util.replace_op(name)
 | 
			
		||||
| 
						 | 
				
			
			@ -818,11 +827,12 @@ fn (g mut JsGen) gen_for_stmt(it ast.ForStmt) {
 | 
			
		|||
fn (g mut JsGen) fn_args(args []table.Arg, is_variadic bool) {
 | 
			
		||||
	// no_names := args.len > 0 && args[0].name == 'arg_1'
 | 
			
		||||
	for i, arg in args {
 | 
			
		||||
		name := js_name(arg.name)
 | 
			
		||||
		is_varg := i == args.len - 1 && is_variadic
 | 
			
		||||
		if is_varg {
 | 
			
		||||
			g.write('...$arg.name')
 | 
			
		||||
			g.write('...$name')
 | 
			
		||||
		} else {
 | 
			
		||||
			g.write(arg.name)
 | 
			
		||||
			g.write(name)
 | 
			
		||||
		}
 | 
			
		||||
		// if its not the last argument
 | 
			
		||||
		if i < args.len - 1 {
 | 
			
		||||
| 
						 | 
				
			
			@ -911,7 +921,6 @@ fn (g mut JsGen) gen_return_stmt(it ast.Return) {
 | 
			
		|||
	g.writeln(';')
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
fn (g mut JsGen) enum_expr(node ast.Expr) {
 | 
			
		||||
	match node {
 | 
			
		||||
		ast.EnumVal {
 | 
			
		||||
| 
						 | 
				
			
			@ -924,7 +933,7 @@ fn (g mut JsGen) enum_expr(node ast.Expr) {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
fn (g mut JsGen) gen_struct_decl(node ast.StructDecl) {
 | 
			
		||||
  	g.writeln('class $node.name {')
 | 
			
		||||
  	g.writeln('class ${js_name(node.name)} {')
 | 
			
		||||
	g.inc_indent()
 | 
			
		||||
	g.writeln(g.doc.gen_ctor(node.fields))
 | 
			
		||||
	g.writeln('constructor(values) {')
 | 
			
		||||
| 
						 | 
				
			
			@ -980,7 +989,7 @@ fn (g mut JsGen) gen_ident(node ast.Ident) {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	// TODO js_name
 | 
			
		||||
	name := node.name
 | 
			
		||||
	name := js_name(node.name)
 | 
			
		||||
	// TODO `is`
 | 
			
		||||
	// TODO handle optionals
 | 
			
		||||
	g.write(name)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -45,7 +45,7 @@ fn (mut d JsDoc) gen_typ(typ, name string) string {
 | 
			
		|||
	d.write('/**')
 | 
			
		||||
	d.write(' @type {$typ}')
 | 
			
		||||
	if name.len > 0 {
 | 
			
		||||
		d.write(' - $name')
 | 
			
		||||
		d.write(' - ${js_name(name)}')
 | 
			
		||||
	}
 | 
			
		||||
	d.write(' */')
 | 
			
		||||
	return d.out.str()
 | 
			
		||||
| 
						 | 
				
			
			@ -77,10 +77,11 @@ fn (mut d JsDoc) gen_fn(it ast.FnDecl) string {
 | 
			
		|||
		}
 | 
			
		||||
		arg_type_name := d.gen.typ(arg.typ)
 | 
			
		||||
		is_varg := i == it.args.len - 1 && it.is_variadic
 | 
			
		||||
		name := js_name(arg.name)
 | 
			
		||||
		if is_varg {
 | 
			
		||||
			d.writeln('* @param {...$arg_type_name} $arg.name')
 | 
			
		||||
			d.writeln('* @param {...$arg_type_name} $name')
 | 
			
		||||
		} else {
 | 
			
		||||
			d.writeln('* @param {$arg_type_name} $arg.name')
 | 
			
		||||
			d.writeln('* @param {$arg_type_name} $name')
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	d.writeln('* @return {$type_name}')
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,14 +1,18 @@
 | 
			
		|||
// V_COMMIT_HASH 83289d7
 | 
			
		||||
// V_CURRENT_COMMIT_HASH fc7e64b
 | 
			
		||||
// V_COMMIT_HASH d60233b
 | 
			
		||||
// V_CURRENT_COMMIT_HASH fc520d9
 | 
			
		||||
 | 
			
		||||
// Generated by the V compiler
 | 
			
		||||
"use strict";
 | 
			
		||||
 | 
			
		||||
const CONSTANTS = Object.freeze({
 | 
			
		||||
	/** @type {number} - i_am_a_const */
 | 
			
		||||
	i_am_a_const: 21214
 | 
			
		||||
	i_am_a_const: 	21214,
 | 
			
		||||
	/** @type {string} - v_super */
 | 
			
		||||
	v_super: "amazing keyword"
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
/* namespace: main */
 | 
			
		||||
const main = (function () {
 | 
			
		||||
class Companies {
 | 
			
		||||
		/**
 | 
			
		||||
		* @param {{google: number, amazon: boolean, yahoo: string}} values - values for this class fields
 | 
			
		||||
| 
						 | 
				
			
			@ -39,15 +43,24 @@ class Companies {
 | 
			
		|||
			
 | 
			
		||||
			return 0;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	const POSITION = Object.freeze({
 | 
			
		||||
		go_back: 0,
 | 
			
		||||
		dont_go_back: 1,
 | 
			
		||||
	});
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
const POSITION = Object.freeze({
 | 
			
		||||
	GO_BACK: 0,
 | 
			
		||||
	DONT_GO_BACK: 1,
 | 
			
		||||
});
 | 
			
		||||
	/**
 | 
			
		||||
	* @param {string} v_extends
 | 
			
		||||
	* @param {number} v_instanceof
 | 
			
		||||
	* @return {void}
 | 
			
		||||
	*/
 | 
			
		||||
	function v_class(v_extends, v_instanceof) {
 | 
			
		||||
		/** @type {number} - v_delete */
 | 
			
		||||
		const v_delete = v_instanceof;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
/* program entry point */
 | 
			
		||||
(async function() {
 | 
			
		||||
	/* program entry point */
 | 
			
		||||
	(async function() {
 | 
			
		||||
		/** @type {string} - v */
 | 
			
		||||
		const v = "done";
 | 
			
		||||
		{
 | 
			
		||||
| 
						 | 
				
			
			@ -56,7 +69,13 @@ const POSITION = Object.freeze({
 | 
			
		|||
		}
 | 
			
		||||
		
 | 
			
		||||
		/** @type {number} - pos */
 | 
			
		||||
	const pos = POSITION.GO_BACK;
 | 
			
		||||
		const pos = POSITION.go_back;
 | 
			
		||||
		/** @type {string} - v_debugger */
 | 
			
		||||
		const v_debugger = "JS keyword";
 | 
			
		||||
		/** @type {string} - v_await */
 | 
			
		||||
		const v_await = CONSTANTS.v_super + v_debugger;
 | 
			
		||||
		/** @type {string} - v_finally */
 | 
			
		||||
		let v_finally = "implemented";
 | 
			
		||||
		/** @type {number} - dun */
 | 
			
		||||
		const dun = CONSTANTS.i_am_a_const * 20;
 | 
			
		||||
		for (let i = 0; i < 10; i++) {
 | 
			
		||||
| 
						 | 
				
			
			@ -71,8 +90,8 @@ const POSITION = Object.freeze({
 | 
			
		|||
		
 | 
			
		||||
		/** @type {number[]} - arr */
 | 
			
		||||
		const arr = [1, 2, 3, 4, 5];
 | 
			
		||||
	for (let tmp1 = 0; tmp1 < arr.length; ++tmp1) {
 | 
			
		||||
		let a = arr[tmp1];
 | 
			
		||||
		for (let _tmp1 = 0; _tmp1 < arr.length; ++_tmp1) {
 | 
			
		||||
			let a = arr[_tmp1];
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		/** @type {Map<string, string>} - ma */
 | 
			
		||||
| 
						 | 
				
			
			@ -90,38 +109,39 @@ const POSITION = Object.freeze({
 | 
			
		|||
			resolve();
 | 
			
		||||
		});
 | 
			
		||||
		
 | 
			
		||||
})();
 | 
			
		||||
	})();
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
	* @param {number} num
 | 
			
		||||
	* @param {string} def
 | 
			
		||||
	* @return {void}
 | 
			
		||||
	*/
 | 
			
		||||
	function async(num, def) {
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
/**
 | 
			
		||||
* @param {number} num
 | 
			
		||||
* @param {string} def
 | 
			
		||||
* @return {void}
 | 
			
		||||
*/
 | 
			
		||||
function async(num, def) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* [inline] */
 | 
			
		||||
/**
 | 
			
		||||
* @param {number} game_on
 | 
			
		||||
* @param {...string} dummy
 | 
			
		||||
* @return {multi_return_int_int}
 | 
			
		||||
*/
 | 
			
		||||
function hello(game_on, ...dummy) {
 | 
			
		||||
	for (let tmp2 = 0; tmp2 < dummy.length; ++tmp2) {
 | 
			
		||||
		let dd = dummy[tmp2];
 | 
			
		||||
	/* [inline] */
 | 
			
		||||
	/**
 | 
			
		||||
	* @param {number} game_on
 | 
			
		||||
	* @param {...string} dummy
 | 
			
		||||
	* @return {multi_return_int_int}
 | 
			
		||||
	*/
 | 
			
		||||
	function hello(game_on, ...dummy) {
 | 
			
		||||
		for (let _tmp2 = 0; _tmp2 < dummy.length; ++_tmp2) {
 | 
			
		||||
			let dd = dummy[_tmp2];
 | 
			
		||||
			/** @type {string} - l */
 | 
			
		||||
			const l = dd;
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		(function defer() {
 | 
			
		||||
		/** @type {string} - do */
 | 
			
		||||
		const do = "not";
 | 
			
		||||
			/** @type {string} - v_do */
 | 
			
		||||
			const v_do = "not";
 | 
			
		||||
		})();
 | 
			
		||||
		return [game_on + 2, 221];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	/* module exports */
 | 
			
		||||
	return {
 | 
			
		||||
	};
 | 
			
		||||
})();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
 | 
			
		||||
const (
 | 
			
		||||
    i_am_a_const = 21214
 | 
			
		||||
    super = 'amazing keyword'
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
struct Companies {
 | 
			
		||||
| 
						 | 
				
			
			@ -10,17 +11,27 @@ struct Companies {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
enum POSITION {
 | 
			
		||||
    GO_BACK
 | 
			
		||||
    DONT_GO_BACK
 | 
			
		||||
    go_back
 | 
			
		||||
    dont_go_back
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn class(extends string, instanceof int) {
 | 
			
		||||
    delete := instanceof
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    v := "done"
 | 
			
		||||
    {
 | 
			
		||||
        _ := "block"
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pos := POSITION.GO_BACK
 | 
			
		||||
    pos := POSITION.go_back
 | 
			
		||||
 | 
			
		||||
    debugger := 'JS keyword'
 | 
			
		||||
    // TODO: Implement interpolation
 | 
			
		||||
    await := super + debugger
 | 
			
		||||
    mut finally := 'implemented'
 | 
			
		||||
 | 
			
		||||
    dun := i_am_a_const * 20
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue