C语言的解释器(3)——lclib.c

longqzh posted @ 2010年7月07日 01:36 in Little C Interpreter with tags c 解释器 C语言解释器 , 2377 阅读

因空间有限,Little C暂只包含5个库函数,分别是:getche(),putch(),puts(),print(),getnum().

 

//
//  lclib.c
//  cinterp
//
//  Created by long on 12. 6. 16..
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

/*  Internal Library Functions */

#include <stdio.h>
#include <stdlib.h>

extern char *prog;
extern char token[80];
extern char token_type;
extern char tok;

enum token_types{
    DELIMITER, IDENTIFIER, NUMBER, COMMAND, STRING,
    QUOTE, VARIABLE, BLOCK, FUNCTION
};

/*  These are the contants used to call sntx_err() when
    a syntax error occurs. Add more if you like.

    NOTE: SYNTAX is a generic error message used when
    nothing else seems appropriate.
*/

enum error_msg{
    SYNTAX, UNBAL_PARENS, NO_EXP, EQUALS_EXPECTED,
    NOT_VAR, PARAM_ERR, SEMI_EXPECTED,
    UNBAL_BRACES, FUNC_UNDEF, TYPE_EXPECTED,
    NEST_FUNC, RET_NOCALL, PAREN_EXPECTED,
    WHILE_EXPECED, QUOTE_EXPECTED, NOT_TEMP,
    TOO_MANY_LVARS
};


int get_token();
void sntx_err(int error);
void eval_exp(int *result);
void putback();

/*  Get a character from console */
char call_getche()
{
    char ch;
    ch=getchar();
    while(*prog!=')')  
        prog++;
    prog++;     /* advance to end of line */
    return ch;
}

/*  Put acharacter to the display */
int call_putch()
{
    int value;
    
    eval_exp(&value);
    printf("%c",value);
    return value;
}

/*  Call puts */
int call_puts()
{
    get_token();
    if(*token!='(')
        sntx_err(PAREN_EXPECTED);
    
    get_token();
    if(token_type!=QUOTE) 
        sntx_err(QUOTE_EXPECTED);
    
    puts(token);
    get_token();
    if(*token!=')')
        sntx_err(PAREN_EXPECTED);
    
    get_token();
    if(*token!=';')
        sntx_err(SEMI_EXPECTED);
    putback();
    return 0;
    
}


/*  A built-in console output function */
int print()
{
    int i;
    get_token();
    if(*token!='(')
        sntx_err(PAREN_EXPECTED);
    get_token();
    
    if(token_type==QUOTE){
        printf("%s",token);
    }
    else{
        putback();
        eval_exp(&i);
        printf("%d",i);
    }
    
    get_token();
    
    if(*token!=')')
        sntx_err(PAREN_EXPECTED);
    
    get_token();
    
    if(*token!=';')
        sntx_err(SEMI_EXPECTED);
    putback();
    return 0;
}


/*  Read an integer from keyboard */
int getnum()
{
    char s[80];
    
    gets(s);
    while(*prog!=')')
        prog++;
    
    prog++;
    return atoi(s);
}

如用Turbo/Borland C/C++,可用以下命令编译。

tcc -c parser.c

tcc -c lclib.c

tcc littlec.c parser.obj lclib.obj

 

Avatar_small
NCERT English Sample 说:
2022年9月27日 14:39

Teaching Staff of Leading Educational Institutes have prepared these NCERT 10th Class English Sample Papers 2023 all important questions which has been repeatedly asked in previous years old exams. NCERT English Sample Paper Class 10These NCERT English Question Bank for Reading, Writing, Grammar and Literature questions are prepared from the newly revised syllabus which is listed here.Teaching Staff of Leading Educational Institutes have prepared these NCERT 10th Class English Sample Papers 2023 all important questions which has been repeatedly asked in previous years old exams.

Avatar_small
civaget 说:
2023年12月07日 05:51

My organic traffic has surged since partnering with 백링크하이. SEO success story!

Avatar_small
civaget 说:
2023年12月10日 23:17

I'm impressed with the user-friendly interface of 티비몬, it's so easy to find and enjoy content.

Avatar_small
civaget 说:
2023年12月13日 18:52

제주레깅스룸 sounds like a fashionista's dream. Looking forward to exploring this trendy lodging option.

Avatar_small
civaget 说:
2023年12月14日 19:41

I've heard great things about the entertainment at 오피. Can't wait to check it out!

Avatar_small
SEO 说:
2023年12月14日 23:35

Trustworthy 메이저사이트 options are vital for bettors. Toto Match's safety commitment ensures carefree online gambling.

Avatar_small
civaget 说:
2023年12月15日 23:14

I'm a firm believer in the power of 힐링. It's an essential part of my self-care routine now.

Avatar_small
civaget 说:
2023年12月16日 14:25

Usually I don’t comment on your blog. I am more of the silent type but I wonder, is this wordpress because I am thinking of switching my own blog from blogspot to wordpress. 해외축구중계

Avatar_small
civaget 说:
2023年12月16日 22:47

Tipping's 설문조사 사이트 is a comprehensive solution for surveys, making data collection and analysis a seamless process.

Avatar_small
civaget 说:
2023年12月17日 23:05

Unwind with 인천출장마사지, the ultimate stress-buster. Tailored massages at your location for a more relaxed and rejuvenated you.

Avatar_small
civaget 说:
2023年12月26日 01:45

Never miss a game-changing moment again. 스포츠중계 delivers sports excitement in real-time.

Avatar_small
civaget 说:
2024年1月15日 16:54

I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I’m quite sure I will learn plenty of new stuff right here! Good luck for the next! คืนยอดเสีย ทุกวัน

Avatar_small
SEO 说:
2024年1月24日 17:56

Wahhhh,!! I dnt think its food thats making her tummy groww!!|tiitaBoo| The Presidential Family

Avatar_small
SEO 说:
2024年1月29日 15:09

My brother recommended I may like this web site. He was entirely right. This publish actually made my day. You can not consider just how so much time I had spent for this information! Thank you! linkbola

Avatar_small
SEO 说:
2024年1月31日 14:53

Wow, superb weblog structure! How long have you been blogging for? you make blogging glance easy. The total look of your web site is excellent, neatly as the content material! panen138

Avatar_small
SEO 说:
2024年1月31日 20:16 I completely understand everything you have said. Actually, I browsed through your additional content articles and I think you happen to be absolutely right. Great job with this online site. The Growth Matrix Reviews
Avatar_small
SEO 说:
2024年1月31日 22:05

Paper rolls… very great read you know alot about this subject i see!… The Billionaire Brain Wave Reviews

Avatar_small
SEO 说:
2024年2月03日 15:51

Nice to be visiting your blog once more, it continues to be months for me. Nicely this post that i’ve been waited for so lengthy. I want this article to total my assignment in the university, and it has same topic together with your post. Thanks, terrific share. acim podcast

Avatar_small
seo 说:
2024年2月07日 17:33

Super-Duper website! I am loving it!! Will be back later to read some more. I am bookmarking your feeds also 에볼루션카지노

Avatar_small
seo 说:
2024年2月11日 13:52

I really appreciate this post. I¡¦ve been looking everywhere for this! Thank goodness I found it on Bing. You've made my day! Thanks again 인계동셔츠룸

Avatar_small
sar 说:
2024年2月12日 18:31

I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. fc sevilla

Avatar_small
Banxso Reviews Hello 说:
2024年3月13日 00:24

"Good day ,
Thank you for reaching out and sharing your concerns with us. We deeply value your feedback and appreciate the opportunity to address the issues you've experienced. We're genuinely sorry to hear that your experience with Banxso has not met your expectations.ÊOur team strives to provide exceptional service and support to all our clients, and we regret any inconvenience or frustration you've encountered. Please accept our sincerest apologies as we take full responsibility for any dissatisfaction caused. Your feedback is crucial to us,Êand we take these concerns very seriously. Upon receiving your complaint, we immediately launched an investigation to understand the root cause and ensure such situations are promptly and effectively addressed. Our commitment to continuous improvement drives us to learn from each interaction. We are determined to resolve this matter to your satisfaction.ÊOur team has already initiated corrective actions, including [specific actions taken], to prevent such issues from occurring in the future. We are committed to making the necessary improvements to serve you better. To assist you further and ensure a more personalized resolution,Êwe invite you to share additional details or contact information through the following methods: * Email: Reach out via email atÊbanxso.com * Facebook Page: Connect with us on our Facebook page atÊhttps://www.facebook.com/Banxsocom/ * Phone (Dedicated for Hello Peter visitors): Give us a call at +27 After our thorough investigation, we will reach out to you by your registered emailÊto discuss our findings and the steps we've taken to address your concerns. Thank you again for bringing this to our attention.ÊYour insights are invaluable in helping us enhance our services. We sincerely hope to have the opportunity to regain your trust and demonstrate our dedication to your satisfaction in the future. Kind regards, The Banxso Team?? "


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter