Skip to content

Latest commit

 

History

11 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go-Expression-Interpreter: 表达式解释器

这是一个用 Go 语言实现的轻量级表达式解释器。

它使用 Pratt 解析 技术将中缀表达式转换为抽象语法树(AST),并进行求值。

功能

  • 解析和计算整数算术表达式。
  • 支持 +, -, *, /, ^ (幂) 等运算符。
  • 正确处理运算符优先级和结合性。
  • 支持括号 () 和负号前缀。
  • 提供一个简单的交互式 REPL。

快速开始

  1. 克隆仓库

    git clone https://github.com/7szho/Go-Expression-Interpreter.git
    cd Go-Expression-Interpreter
  2. 运行

    go run .

使用示例

程序启动后,在 >> 提示符后输入表达式即可:

>> 5 * (2 + 3)
25
>> 2^3 + 1
9
>> -10 / 5
-2

项目结构

Go-Expression-Interpreter/
├── go.mod
├── main.go      # 程序入口和 REPL
├── token/       # Token 定义
├── ast/         # 抽象语法树
├── lexer/       # 词法分析器
├── parser/      # Pratt 解析器
├── object/      # 运行时对象
└── evaluator/   # 求值器

About

This is a lightweight expression interpreter implemented in golang.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages