Skip to content

Add multi context #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added py.CompileMode instead of string
  • Loading branch information
Drew O'Meara committed Feb 3, 2022
commit fc528c7da59f94df4ec9fcf45c9c9100c07ce7b2
23 changes: 13 additions & 10 deletions compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,33 +89,36 @@ func init() {
py.Compile = Compile
}

// Compile(source, filename, mode, flags, dont_inherit) -> code object
// Compile(src, srcDesc, compileMode, flags, dont_inherit) -> code object
//
// Compile the source string (a Python module, statement or expression)
// into a code object that can be executed by exec() or eval().
// The filename will be used for run-time error messages.
// The mode must be 'exec' to compile a module, 'single' to compile a
// single (interactive) statement, or 'eval' to compile an expression.
// into a code object that can be executed.
//
// srcDesc is used for run-time error messages and is typically a file system pathname,
//
// See py.CompileMode for compile mode options.
//
// The flags argument, if present, controls which future statements influence
// the compilation of the code.
//
// The dont_inherit argument, if non-zero, stops the compilation inheriting
// the effects of any future statements in effect in the code calling
// compile; if absent or zero these statements do influence the compilation,
// in addition to any features explicitly specified.
func Compile(str, filename, mode string, futureFlags int, dont_inherit bool) (py.Object, error) {
func Compile(src, srcDesc string, mode py.CompileMode, futureFlags int, dont_inherit bool) (*py.Code, error) {
// Parse Ast
Ast, err := parser.ParseString(str, mode)
Ast, err := parser.ParseString(src, mode)
if err != nil {
return nil, err
}
// Make symbol table
SymTable, err := symtable.NewSymTable(Ast, filename)
SymTable, err := symtable.NewSymTable(Ast, srcDesc)
if err != nil {
return nil, err
}
c := newCompiler(nil, compilerScopeModule)
c.Filename = filename
err = c.compileAst(Ast, filename, futureFlags, dont_inherit, SymTable)
c.Filename = srcDesc
err = c.compileAst(Ast, srcDesc, futureFlags, dont_inherit, SymTable)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion compile/compile_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

var compileTestData = []struct {
in string
mode string // exec, eval or single
mode py.CompileMode
out *py.Code
exceptionType *py.Type
errString string
Expand Down
2 changes: 1 addition & 1 deletion parser/grammar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var debugLevel = flag.Int("debugLevel", 0, "Debug level 0-4")
func TestGrammar(t *testing.T) {
SetDebug(*debugLevel)
for _, test := range grammarTestData {
Ast, err := ParseString(test.in, test.mode)
Ast, err := ParseString(test.in, py.CompileMode(test.mode))
if err != nil {
if test.exceptionType == nil {
t.Errorf("%s: Got exception %v when not expecting one", test.in, err)
Expand Down
16 changes: 8 additions & 8 deletions parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,20 @@ type yyLex struct {
// can be 'exec' if source consists of a sequence of statements,
// 'eval' if it consists of a single expression, or 'single' if it
// consists of a single interactive statement
func NewLex(r io.Reader, filename string, mode string) (*yyLex, error) {
func NewLex(r io.Reader, filename string, mode py.CompileMode) (*yyLex, error) {
x := &yyLex{
reader: bufio.NewReader(r),
filename: filename,
indentStack: []int{0},
state: readString,
}
switch mode {
case "exec":
case py.ExecMode:
x.queue(FILE_INPUT)
x.exec = true
case "eval":
case py.EvalMode:
x.queue(EVAL_INPUT)
case "single":
case py.SingleMode:
x.queue(SINGLE_INPUT)
x.interactive = true
default:
Expand Down Expand Up @@ -933,7 +933,7 @@ func SetDebug(level int) {
}

// Parse a file
func Parse(in io.Reader, filename string, mode string) (mod ast.Mod, err error) {
func Parse(in io.Reader, filename string, mode py.CompileMode) (mod ast.Mod, err error) {
lex, err := NewLex(in, filename, mode)
if err != nil {
return nil, err
Expand All @@ -952,12 +952,12 @@ func Parse(in io.Reader, filename string, mode string) (mod ast.Mod, err error)
}

// Parse a string
func ParseString(in string, mode string) (ast.Ast, error) {
func ParseString(in string, mode py.CompileMode) (ast.Ast, error) {
return Parse(bytes.NewBufferString(in), "<string>", mode)
}

// Lex a file only, returning a sequence of tokens
func Lex(in io.Reader, filename string, mode string) (lts LexTokens, err error) {
func Lex(in io.Reader, filename string, mode py.CompileMode) (lts LexTokens, err error) {
lex, err := NewLex(in, filename, mode)
if err != nil {
return nil, err
Expand All @@ -984,6 +984,6 @@ func Lex(in io.Reader, filename string, mode string) (lts LexTokens, err error)
}

// Lex a string
func LexString(in string, mode string) (lts LexTokens, err error) {
func LexString(in string, mode py.CompileMode) (lts LexTokens, err error) {
return Lex(bytes.NewBufferString(in), "<string>", mode)
}
2 changes: 1 addition & 1 deletion parser/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func TestLex(t *testing.T) {
for _, test := range []struct {
in string
errString string
mode string
mode py.CompileMode
lts LexTokens
}{
{"", "", "exec", LexTokens{
Expand Down
2 changes: 1 addition & 1 deletion symtable/symtable_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

var symtableTestData = []struct {
in string
mode string // exec, eval or single
mode py.CompileMode
out *SymTable
exceptionType *py.Type
errString string
Expand Down