CI-LISP

A custom Lisp interpreter built in C using Flex and Bison.

Project Overview

CI-LISP is a minimalist Lisp interpreter developed in C that utilizes Flex for lexical analysis and Bison for parsing. This project demonstrates how to build an interpreter that can evaluate arithmetic expressions and handle function calls using an Abstract Syntax Tree (AST).

This was a school solo project, a shell of the .c,.y,.l, and .h files was provided and I completed the rest, it was not written from scratch.

Implementation Details

Components

Key Functions and Features

The project features a modular design where:

Code Snippets

Main Evaluation Function


// Evaluates an AST node and returns its computed value
RET_VAL eval(AST_NODE *node) {
    if (!node) {
        yyerror("NULL ast node passed into eval!");
        return NAN_RET_VAL;
    }
    switch (node->type) {
        case NUM_NODE_TYPE:
            return evalNumNode(node);
        case FUNC_NODE_TYPE:
            return evalFuncNode(node);
        default:
            warning("Unknown node type in eval");
            return NAN_RET_VAL;
    }
}
        

Flex Lexer (cilisp.l)


{double_pattern} {
    llog(DOUBLE);
    yylval.dval = strtod(yytext, NULL);
    return DOUBLE;
}

{int} {
    llog(INT);
    yylval.dval = strtod(yytext, NULL);
    return INT;
}
        

Bison Grammar Rule (cilisp.y)


s_expr:
    f_expr {
        $$ = $1;
    }
    | number {
        $$ = $1;
    }
    | QUIT {
        exit(EXIT_SUCCESS);
    }
    ;
        

Features

Usage

To compile CI-LISP, ensure you have Flex and Bison installed. Then run:


        yacc -d cilisp.y
        lex cilisp.l
        cat cilisp.c lex.yy.c y.tab.c > t.c
        gcc t.c -o cilisp -lm -lfl
        
      

Run the interpreter with:


./cilisp
      

CI-LISP will read from standard input.

Resources

For more details visit the GitHub repository.

View on GitHub
Back to Portfolio