analyse

package
v0.0.0-...-5f0a774 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package analyse implements static analysis of Lox programs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckSemantics

func CheckSemantics(program *ast.Program, opts ...Option) error

CheckSemantics checks that the following rules have been followed:

  • Write-only properties are not allowed
  • break and continue can only be used inside a loop
  • return can only be used inside a function definition
  • init() cannot return a value
  • init() cannot be static
  • _ cannot be used as a value
  • _ cannot be used as a field name
  • this can only be used inside a method definition
  • super can only be used inside a method definition
  • super can only be used inside a subclass
  • super properties cannot be assigned to
  • property getter cannot have parameters
  • property setter must have exactly one parameter
  • functions cannot have more than 255 parameters
  • function calls cannot have more than 255 arguments
  • classes cannot inherit from themselves
  • classes cannot have two methods with the same name and modifiers
  • classes cannot have a property accessor and method with the same name

If there is an error, it will be of type loxerr.Errors.

func InheritanceChain

func InheritanceChain(decl *ast.ClassDecl, identBindings map[*ast.Ident][]ast.Binding) iter.Seq[*ast.ClassDecl]

InheritanceChain returns an iterator over the chain of classes used to look up possibly inherited properties. Iteration starts from the given class declaration, then successive iterations traverse its superclasses. identBindings is used to superclass identifiers to their declarations. This will typically be the result of ResolveIdents.

func Program

func Program(program *ast.Program, builtins []ast.Decl, opts ...Option) error

Program performs static analysis of a program and reports any errors detected. builtins is a list of built-in declarations which are available in the global scope. The analyses performed are described in the doc comments for ResolveIdents and CheckSemantics. If there is an error, it will be of type loxerr.Errors.

func ResolveIdents

func ResolveIdents(program *ast.Program, builtins []ast.Decl, opts ...Option) (map[*ast.Ident][]ast.Binding, error)

ResolveIdents resolves the identifiers in a program to their bindings. It returns a map from identifier to its bindings. There will be multiple bindings associated with a single identifier if it's not possible to determine which binding the identifier refers to. If an error is returned then a possibly incomplete map will still be returned along with it. The error will be of type loxerr.Errors. builtins is a list of built-in declarations which are available in the global scope.

This function also checks that identifiers are not:

  • declared and never used
  • declared more than once in the same scope
  • used before they are declared (best effort for globals)
  • used and not declared (best effort for globals)
  • used before they are defined (best effort for globals)

Some checks are best effort for global identifiers as it's not always possible to determine how they're used without running the program. For example, in the following example, whether the program is valid depends on whether the global variable x is defined before printX is called.

fun printX() {
    print x;
}
var x = 1;
printX();

Example

Given the following code:

1 | class Foo {
2 |   method() {}
3 |
4 |   otherMethod() {
5 |     this.method();
6 |   }
7 | }
8 |
9 | class Bar {
10|   method() {}
11| }
12|
13| var foo = Foo();
14| print foo;
15| foo.method();

The returned map is:

{
  1:7: Foo => [ 1:1: class Foo { ... } ],
  2:3: method => [ 2:3: method() {} ],
  4:3: otherMethod => [ 4:3: otherMethod() { ... } ],
  5:10: method => [ 2:3: method() {} ],
  9:7: Bar => [ 9:7: class Bar { ... } ],
  10:3: method => [ 10:3: method() {} ],
  13:5: foo => [ 13:1: var foo = Foo(); ],
  13:11: Foo => [ 1:1: class Foo { ... } ],
  14:7: foo => [ 13:1: var foo = Foo(); ],
  15:1: foo => [ 13:1: var foo = Foo(); ],
  15:5: method => [ 2:3: method() {}, 10:3: method() {} ],
}

Types

type Option

type Option func(*config)

Option can be passed to Program, ResolveIdents, and CheckSemantics to configure analysis behaviour.

func WithExtraFeatures

func WithExtraFeatures(enabled bool) Option

WithExtraFeatures enables extra features that https://github.com/marcuscaisey/lox implements but the base Lox language does not. Extra features are enabled by default.

func WithFatalOnly

func WithFatalOnly(enabled bool) Option

WithFatalOnly configures only fatal errors to be reported.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL