compiler: clean up. remove new_parser_string_id (#2140)

* clean up

* fix comment

* fix comment
pull/2141/head^2
joe-conigliaro 2019-09-28 11:56:05 +10:00 committed by Alexander Medvednikov
parent a5391c8882
commit 802da8f31d
2 changed files with 7 additions and 11 deletions

View File

@ -335,7 +335,7 @@ fn (v mut V) compile() {
} }
} }
// parse generated V code (str() methods etc) // parse generated V code (str() methods etc)
mut vgen_parser := v.new_parser_string_id(v.vgen_buf.str(), 'vgen') mut vgen_parser := v.new_parser_string(v.vgen_buf.str(), 'vgen')
// free the string builder which held the generated methods // free the string builder which held the generated methods
v.vgen_buf.free() v.vgen_buf.free()
vgen_parser.parse(.main) vgen_parser.parse(.main)

View File

@ -7,7 +7,6 @@ module main
import ( import (
os os
strings strings
crypto.sha1
) )
// TODO rename to Token // TODO rename to Token
@ -95,15 +94,10 @@ const (
MaxModuleDepth = 4 MaxModuleDepth = 4
) )
// new parser from string. parser id will be hash of s // new parser from string. unique id specified in `id`.
fn (v mut V) new_parser_string(text string) Parser { // tip: use a hashing function to auto generate `id` from `text` eg. sha1.hexhash(text)
return v.new_parser_string_id(text, sha1.hexhash(text)) fn (v mut V) new_parser_string(text string, id string) Parser {
}
// new parser from string. with id specified in `id`
fn (v mut V) new_parser_string_id(text string, id string) Parser {
mut p := v.new_parser(new_scanner(text), id) mut p := v.new_parser(new_scanner(text), id)
p.import_table = v.table.get_file_import_table(id)
p.scan_tokens() p.scan_tokens()
v.add_parser(p) v.add_parser(p)
return p return p
@ -128,7 +122,6 @@ fn (v mut V) new_parser_file(path string) Parser {
file_name: path.all_after('/'), file_name: path.all_after('/'),
file_platform: path_platform, file_platform: path_platform,
file_pcguard: path_pcguard, file_pcguard: path_pcguard,
import_table: v.table.get_file_import_table(path),
is_script: (v.pref.is_script && path == v.dir) is_script: (v.pref.is_script && path == v.dir)
} }
v.cgen.file = path v.cgen.file = path
@ -139,6 +132,8 @@ fn (v mut V) new_parser_file(path string) Parser {
return p return p
} }
// creates a new parser. most likely you will want to use
// `new_parser_file` or `new_parser_string` instead.
fn (v mut V) new_parser(scanner &Scanner, id string) Parser { fn (v mut V) new_parser(scanner &Scanner, id string) Parser {
mut p := Parser { mut p := Parser {
id: id id: id
@ -152,6 +147,7 @@ fn (v mut V) new_parser(scanner &Scanner, id string) Parser {
os: v.os os: v.os
vroot: v.vroot vroot: v.vroot
local_vars: [Var{}].repeat(MaxLocalVars) local_vars: [Var{}].repeat(MaxLocalVars)
import_table: v.table.get_file_import_table(id)
} }
$if js { $if js {
p.is_js = true p.is_js = true