frontend: parser: Add parser groundwork
Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
@@ -12,6 +12,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "common/knobs.h"
|
#include "common/knobs.h"
|
||||||
#include "common/state.h"
|
#include "common/state.h"
|
||||||
|
#include "frontend/parser.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
@@ -28,6 +29,10 @@ main(int argc, char **argv)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (parser_parse(&state) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
quip_state_destroy(&state);
|
quip_state_destroy(&state);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
58
frontend/parser.c
Normal file
58
frontend/parser.c
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026, Mirocom Laboratories
|
||||||
|
* Provided under the BSD-3 clause
|
||||||
|
*
|
||||||
|
* Abstract:
|
||||||
|
* This file implements the parser.
|
||||||
|
* Author:
|
||||||
|
* Ian M. Moffett <ian@mirocom.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "frontend/parser.h"
|
||||||
|
#include "frontend/lexer.h"
|
||||||
|
#include "frontend/token.h"
|
||||||
|
|
||||||
|
/* Symbolic token */
|
||||||
|
#define symtok(tokstr) \
|
||||||
|
"[" tokstr "]"
|
||||||
|
|
||||||
|
/* Quoted string */
|
||||||
|
#define qtok(tokstr) \
|
||||||
|
"'" tokstr "'"
|
||||||
|
|
||||||
|
/* Convert token to string */
|
||||||
|
#define tokstr1(tt) \
|
||||||
|
toktab[(tt)]
|
||||||
|
|
||||||
|
/* Convert token to string */
|
||||||
|
#define tokstr(tok) \
|
||||||
|
tokstr1((tok)->type)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Table used to convert token constants into human
|
||||||
|
* readable strings for debugging
|
||||||
|
*/
|
||||||
|
static const char *toktab[] = {
|
||||||
|
[TT_NONE] = symtok("none"),
|
||||||
|
[TT_NAME] = symtok("name"),
|
||||||
|
[TT_NEWLINE] = symtok("newline"),
|
||||||
|
[TT_COLON] = qtok(":"),
|
||||||
|
[TT_COLONDUB] = qtok("::")
|
||||||
|
};
|
||||||
|
|
||||||
|
int
|
||||||
|
parser_parse(struct quip_state *state)
|
||||||
|
{
|
||||||
|
struct token token;
|
||||||
|
|
||||||
|
if (state == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (lexer_scan(state, &token) == 0) {
|
||||||
|
printf("got token %s\n", tokstr(&token));
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
25
include/frontend/parser.h
Normal file
25
include/frontend/parser.h
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026, Mirocom Laboratories
|
||||||
|
* Provided under the BSD-3 clause
|
||||||
|
*
|
||||||
|
* Abstract:
|
||||||
|
* This file implements the parser.
|
||||||
|
* Author:
|
||||||
|
* Ian M. Moffett <ian@mirocom.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FRONTEND_PARSER_H
|
||||||
|
#define FRONTEND_PARSER_H 1
|
||||||
|
|
||||||
|
#include "common/state.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Begin parsing the source file
|
||||||
|
*
|
||||||
|
* @state: Quip state machine
|
||||||
|
*
|
||||||
|
* Returns zero on success
|
||||||
|
*/
|
||||||
|
int parser_parse(struct quip_state *state);
|
||||||
|
|
||||||
|
#endif /* !FRONTEND_PARSER_H */
|
||||||
Reference in New Issue
Block a user