analysis

package
v0.0.0-...-405c5eb Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package analysis implements functions to analyze syntactically valid Lox programs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckSemantics

func CheckSemantics(program ast.Program) lox.Errors

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
  • _ cannot be used as a value
  • _ cannot be used as a field name
  • this can only be used inside a method definition
  • 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

func ResolveIdents

func ResolveIdents(program ast.Program, opts ...ResolveIdentsOption) (map[ast.Ident]ast.Ident, lox.Errors)

ResolveIdents resolves the identifiers in a program to their declarations. It returns a map from identifiers to the identifier which declares them. If an error is returned then a possibly incomplete map will still be returned along with it.

For example, given the following code:

1| var a = 1;
2|
3| print a;
4| print a + 1;

The returned map is:

{
  1:5: a [Ident] => 1:5: a [Ident],
  3:7: a [Ident] => 1:5: a [Ident],
  4:7: a [Ident] => 1:5: a [Ident],
}

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 (easily) 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();

Types

type ResolveIdentsOption

type ResolveIdentsOption func(*identResolver)

ResolveIdentsOption can be passed to ResolveIdents to configure the resolving behaviour.

func WithREPLMode

func WithREPLMode() ResolveIdentsOption

WithREPLMode configures identifiers to be resolved in REPL mode. In REPL mode, the following identifier checks are disabled:

  • declared and never used
  • declared more than once in the same scope
  • used before they are declared

Jump to

Keyboard shortcuts

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